Wordpress 如何在 wp_query 中为 Post__not_in 参数使用两个变量

Wordpress how to use two variables for Post__not_in argument in a wp_query

如何将两个变量放入 post__not_in 中,如下所示:

'post__not_in' => $excludeHome, $excludePostSlide

当我这样做时,只有来自第一个变量的帖子不会显示,而其他变量不会显示。

我可以像下面那样使用数组推送来做到这一点,但是有什么不同的方法吗?

  @php
    wp_reset_postdata();
    global $excludeHome;
    global $excludePostSlide;
    array_push($excludeHome, $excludePostSlide[0], $excludePostSlide[1], $excludePostSlide[2], $excludePostSlide[3], $excludePostSlide[4], $excludePostSlide[5]);
    $args = [
        'post_type' => 'post',
        'orderby' => 'date',
        'category_name' => 'auto',
        'posts_per_page' => 6,
        'no_found_rows' => true,
        'post__not_in' => $excludeHome,
    ];
    $querySlider = new WP_Query($args);
  @endphp

尝试将参数传递为 array :

$args = [
    'post_type' => 'post',
    'orderby' => 'date',
    'category_name' => 'auto',
    'posts_per_page' => 6,
    'no_found_rows' => true,
    'post__not_in' => array(
          $excludeHome, 
          $excludePostSlide
     )
];
$querySlider = new WP_Query($args);

根据 wp_queryDocspost__not_in 接受 post 个 ID 数组。

  • 不清楚你的全局 $excludeHome$excludePostSlide 变量实际上是数组。所以确保它们是数组。
  • 为了使用 array_push 函数,最好在 $excludePostSlide 数组上使用 foreach 循环,而不是试图按索引号推送它们!
global $excludeHome;
global $excludePostSlide;

$excludeHome = is_array($excludeHome)
    ? $excludeHome
    : (array)$excludeHome;

$excludePostSlide = is_array($excludePostSlide)
    ? $excludePostSlide
    : (array)$excludePostSlide;

foreach ($excludePostSlide as $excludeSlide) {
    array_push($excludeHome, $excludeSlide);
}

$args = [
    'post_type'      => 'post',
    'orderby'        => 'date',
    'category_name'  => 'auto',
    'posts_per_page' => 6,
    'no_found_rows'  => true,
    'post__not_in'   => $excludeHome,
];

$querySlider = new WP_Query($args);