Can I use the "exclude" array in the wp_get_recent_posts function to exclude Featured Posts? I have a plugin called NS Featured Posts which pulls featured posts through a key in the wp query i.e.:
$custom_query = new WP_Query(
array(
'post_type' => 'post',
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes'
)
);
could I somehow use this to target and exclude NS Featured Posts in the wp_get_recent_posts call e.g.:
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 3,
'exclude' => (the ns featured posts)
));
Thanks for any insight.
So I tested it now and getting posts without a certain meta key in one query is not possible.
But: You can exclude them from another query like so:
$featured_posts = get_posts( [
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'fields' => 'ids',
] );
query_posts( array( 'post__not_in' => $featured_posts ) );
while ( have_posts() ) : the_post();
$output .= '<li>'.get_the_title().'</li>';
endwhile;
wp_reset_query();
Functions such as wp_get_recent_posts() can accept all of the same arguments as WP_Query. While the documentation only lists a handful of parameters, the full set are available to you.
You had suggested using exclude in your query however that's going to want the IDs of the posts to exclude. You could of course grab those first but that's not going to be the most efficient solution.
The way to do this in a single query is with meta query options. The posts are being tagged with a meta key and a meta query will allow you to exclude those. You'll need to check both for the existence of the meta key and the value being 'yes'.
Example:
$recent_posts = wp_get_recent_posts( array(
'numberposts' => 3,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
'compare' => '!=',
),
array(
'key' => '_is_ns_featured_post',
'compare' => 'NOT EXISTS',
),
)
) );
Reference: https://codex.wordpress.org/Class_Reference/WP_Meta_Query
Related
Can't wrap my head around this one, would appreciate some help!
Overview
I have a "news" page with two seperate WP_Query loops. The first loop at the top of the page contains the 2 latest "featured" posts, defined by the following conditions;
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'posts_per_page' => 2,
'meta_query' => array(
array(
'key' => 'featured_post',
'value' => 'yes',
'compare' => 'LIKE',
),
),
);
The "featured_post" key relates to a custom ACF checkbox field. The client uses this to define featured posts in the backend of Wordpress.
Now in the second WP_Query loop, the remaining posts are displayed using the following conditions;
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'featured_post',
'value' => 'yes',
'compare' => 'NOT LIKE',
),
),
);
This essentially excludes the featured posts from this second loop.
Problem
My issue is the client doesn't always remember to untick the "featured" ACF field from the old featured posts when adding a new one. Therefore older featured posts that are no longer meant to be featured don't shift into the second loop.
Is there a way to use the meta_query in the second loop to exclude only the two latest featured posts - instead of all of them?
Or is there a better way?
I hope this will help you.
First, create a function on function.php like this:
<?php
function exclude_featured_post_IDs(){
$featured_post_IDs = array();
$args_featured = array(
'post_type' => 'post',
'orderby' => 'date',
'order' =>'DESC',
'posts_per_page' => 2,
'meta_query' => array(
array(
'key' => 'featured_post',
'value' => '"yes"',
'compare' => 'LIKE'
),
)
);
$query_featured = new WP_Query($args_featured);
if ($query_featured->have_posts()) :
while ($query_featured->have_posts()) :
$query_featured->the_post();
array_push($featured_post_IDs, get_the_ID());
?>
<?php
endwhile;
endif;
wp_reset_postdata();
return $featured_post_IDs;
} ?>
Then, Use this parameter 'post__not_in' => exclude_featured_post_IDs(), in the second WP_Query loop:
<?php
$args = array(
'post_type' => 'post',
'orderby' => 'date',
**'post__not_in' => exclude_featured_post_IDs(),**
'posts_per_page' => -1,
);
?>
I'm trying to figure out a way to exclude posts that are marked as "featured" from the "recent posts" page.
As you can see below, my recent posts page only shows posts from 2 categories (cat1 and cat2). The problem is, once in awhile, I'll add a post to one of the categories below, and mark it as "featured link" or whatever. So when you view the page, you basically see the featured post in the header(OUT of loop), and within the body (IN the loop). How would I go about removing the featured post that is IN the loop?
<?php
// args query
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'order' => 'DESC',
// display only posts in specifics categories (slug)
'category_name' => 'cat1, cat2'
);
// custom query
$recent_posts = new WP_Query($args);
// check that we have results
if($recent_posts->have_posts()) : ?>
<?php
// start loop
while ($recent_posts->have_posts() ) : $recent_posts->the_post();
?>
For anyone wondering, I tried the following, but it didn't work:
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'order' => 'DESC',
// display only posts in specifics categories (slug)
'category_name' => 'cat1, cat2',
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
'compare' => 'NOT LIKE',
),
),
);
When adding the 'meta_query' thing that others suggested, it seems the page stops displaying results all together.
Any idea of how to correctly write this out and maybe offer additional alternative solutions too?
Thanks.
Tested working good. just replace 'compare' => 'NOT LIKE' to 'compare' => 'NOT EXISTS'. Hope this help you.
// args
$args = array(
'numberposts' => 3,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
'compare' => 'NOT EXISTS',
),
),
);
// query
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post();
echo the_title();
endwhile;
endif;
wp_reset_query();
Another way is that you can use 'post__not_in' => array (//post ids you want to exclude) in your wp_query
I have a query which gives me custom post type posts which are sorted by category and a custom fields which has a number that indicates the amount of votes for the post. The problem is, if 2 posts have 3 votes, sometimes when you refresh, it changes their position. Here is the code:
$args = array(
'numberposts' => 10,
'post_type' => 'nominees',
'meta_query' => array(
array(
'key' => 'category',
'value' => get_the_ID(),
'compare' => '=',
)
),
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'count_nominees'
);
I tried adding order on the category meta query, but nothing changes, I still sometimes get different results when refreshing, if the posts have same amount of votes. What is the right way to add second order, by ID or something?
Thanks.
As mentioned in the comments, I think this might help, but you might be able to extend it to be a little more specific in the search query for your use case.
$args = array(
'numberposts' => 10,
'post_type' => 'nominees',
'meta_query' => array(
array(
'key' => 'category',
'value' => get_the_ID(),
'compare' => '='
)
),
'meta_key' => 'count_nominees',
'orderby' => array(
'count_nominees' => 'DESC',
'ID' => 'DESC'
)
);
That should get 10 posts in the nominees post type, only if they're part of category xyz, and have post meta of count_nominees and order by count_nominees first in descending order and then by the post ID in descending order.
You can use the WP_Query documentation on the WordPress codex for more information about complex queries using post meta data.
I am creating a search function for a custom post type on my wordpress site, and I need to filter out search results based off an ACF True/False field. I have to use WP_Query to pass arguments since the generic wordpress loop does not allow that, but when I use WP_Query the query returns all the posts based on the arguments I passed, and disregards the actual search term.
<?php $args = array(
'post_type' => 'work',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'work_hidden',
'value' => '0',
'compare' => '=='
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) {
$the_query->the_post();
include "partials/work-card.php";
}
endif; ?>
How can I use WP_Query to include the search term and the arguments.
Thanks so much!
First the 'compare' when you want equals in SQL MUST be a single '=';
Next you have to put the search parameter as from the Documentation, assuming is $_GET['search']:
$args = array(
'post_type' => 'work',
'posts_per_page' => -1,
's' => $_GET['search'],
'meta_query' => array(
array(
'key' => 'work_hidden',
'value' => '0',
'compare' => '='
)
)
);
I am attempting to use get_posts() to retrieve posts that have certain topics attached to them, in my custom post type named 'leaders'
Here is my query.
$args = array(
'post-type' => 'leaders',
'meta_query' => array(
array(
'key' => 'topics',
'value' => '1773',
'compare' => 'LIKE'
)
)
);
$test = get_posts( $args );
If I var_dump the posts meta data i get this
topics a:4:{i:0;s:4:"1773";i:1;s:4:"1783";i:2;s:4:"1763";i:3;s:4:"1753";}
However my get_posts just returns null, can anyone see why?