简体   繁体   中英

WP_Query exclude posts using meta query

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,
      );
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM