John Davidson

php - WP_Query 'post__not_in' condition is not working: shows all posts


thank you for taking the time to review this.

I am calling a new WP_Query with the following arguments:

$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'post__not_in' => array($featured_post_ID),
'category_name' => 'blog',
'offset' => $offset,
'posts_per_page' => 3,
'orderby' => 'post-date',
'order' => 'DESC',

I know that past issues have been caused by passing a numeric post ID as a variable instead of string; to get around that I am using the following variable definition for $featured_post_ID:

$featured_post_ID = '"'.$post_ID.'"';

When I run this query, it outputs the post I am trying to exclude.

Here is a Transcript of output of the WP_Query to save space. Looks like post__not_in is recognized in the query, but has no effect on the output.

Can anyone advise why this might be happening, or how to fix it?

My Answer:
WP_Query is a class in WordPress that allows developers to query the WordPress database for posts, pages, custom post types, and other content types. It is a powerful tool for creating custom queries and displaying specific content on a WordPress site.

Here is an example of how to use WP_Query to display posts on a WordPress site:

$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC'

$query = new WP_Query( $args );

if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
// Display post content here
} else {
// No posts found


In this example, we are querying the database for 5 posts of the post type, ordered by date in descending order. We then loop through the posts and display the title and content of each post.

WP_Query provides a wide range of parameters that can be used to customize the query, such as post type, category, tag, author, date, and more. It is a versatile tool for creating custom queries in WordPress.

Rate this post

3 of 5 based on 9788 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us