简体   繁体   English

WP_Query 使用元查询排除帖子

[英]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.我有一个带有两个单独的 WP_Query 循环的“新闻”页面。 The first loop at the top of the page contains the 2 latest "featured" posts, defined by the following conditions;页面顶部的第一个循环包含 2 个最新的“精选”帖子,由以下条件定义;

    $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. “featured_post”键与自定义 ACF 复选框字段相关。 The client uses this to define featured posts in the backend of Wordpress.客户端使用它在 Wordpress 的后端定义特色帖子。

Now in the second WP_Query loop, the remaining posts are displayed using the following conditions;现在在第二个 WP_Query 循环中,使用以下条件显示剩余的帖子;

      $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.我的问题是客户在添加新帖子时并不总是记得从旧的精选帖子中取消勾选“精选”ACF 字段。 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?有没有办法在第二个循环中使用 meta_query 来仅排除两个最新的特色帖子 - 而不是全部?

Or is there a better way?或者,还有更好的方法?

I hope this will help you.我希望这能帮到您。
First,第一的,
create a function on function.php like this:在 function.php 上创建一个 function,如下所示:

<?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:在第二个 WP_Query 循环中使用此参数'post__not_in' => exclude_featured_post_IDs()

<?php 
$args = array(
        'post_type' => 'post',
        'orderby' => 'date',
        **'post__not_in' => exclude_featured_post_IDs(),**
        'posts_per_page' => -1,
      );
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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