1

I need to modify the main loop (index.php) to exclude posts that have a particular meta value set (via an ACF True / False field).

function exclude_featured_post( $query ) {
    if ( $query->is_home() && $query->is_main_query()) {
        // in case for some reason there's already a meta query set from other plugin
        $meta_query = $query->get('meta_query')? : [];

        // append yours
        $meta_query[] = [
            'key' => 'featured_post',
            'value' => '1',
            'compare' => '!='
        ];

        $query->set('meta_query', $meta_query);
    }
}
add_action( 'pre_get_posts', 'exclude_featured_post' );

This returns no posts. I am thinking I need to merge this with the main query, something like:

global $wp_query;
$merged_args = array_merge( $wp_query->query, $exclude_featured_args );
$query->set('meta_query', $merged_args);

However, this also returns no posts.

Update:

Using 'compare' => 'NOT EXISTS` instead of 'compare' => '!=' seems to return all posts and exclude the meta_query.

function exclude_featured_post( $query ) {
    if ( $query->is_home() && $query->is_main_query()) {
        // in case for some reason there's already a meta query set from other plugin
        $meta_query = $query->get('meta_query')? : [];

        // append yours
        $meta_query[] = [
            'key' => 'featured_post',
            'value' => '1',
            'compare' => 'NOT EXISTS'
        ];

        $query->set('meta_query', $meta_query);

    }
}
add_action( 'pre_get_posts', 'exclude_featured_post' );

From here: https://core.trac.wordpress.org/ticket/18158

0

Dave, your code seems good except a couple things in it, so please try it:

function exclude_featured_post( $query ) {
    if ( is_front_page() && $query->is_main_query() ) {


        $meta_query = $query->get('meta_query') ? $query->get('meta_query') : array();

        // append yours
        $meta_query[] = array(
            'key' => 'featured_post', // please make sure that key is correct
            'value' => '1',
            'compare' => '!=' // you can also try 'NOT EXISTS' comparison
        );

        $query->set('meta_query', $meta_query);

    }
}
add_action( 'pre_get_posts', 'exclude_featured_post' );

If the above code doesn't work for you, please follow these steps:

  1. Make sure that it is running for homepage main query - just try to echo something inside it or so - this step help you to understand if the condition on line 2 is correct.

  2. Try to play with more simple params like orderby etc. Check if it works.

  3. Test this code for posts which doesn't have another meta_query parameters

  4. Try to use meta_key, meta_value and meta_compare params instead of meta_query, example you can find here https://rudrastyh.com/wordpress/meta_query.html

  5. And make sure that key feature_post is correct, it hard to believe but among my students it is the most common mistake.

  • Oh, I see your updated answer :) Ok, if you use NOT EXISTS and it works, you can skip the value parameter. – Misha Rudrastyh Oct 30 '17 at 8:38
  • Thanks Misha, I see you had the NOT EXISTS comparison suggestion, thank you, this worked for me. – Dave Oct 30 '17 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.