WP查询POST__IN未返回正确结果

时间:2019-05-03 作者:php-b-grader

在我的页面底部,我试图根据预先选择的帖子ID列表显示一些最近的项目。

我构建了一个自定义查询,以尝试检索页面底部的3篇特定帖子(其中custom\\u post\\u type=project)

$projectIDs = array( [0] => 79, [1] => 98, [2] => 108 );

$args = array( \'post_type\' => \'projects\', \'post__in \' => $projectIDs, \'posts_per_page\' => 3 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
endwhile;
问题:它返回了3个完全不同的帖子:110、108、111-甚至没有返回其中一个。我可以在查询中看到它过滤post\\u类型,但没有针对post\\u的过滤器。。。

我不太喜欢WP查询,所以不确定我是否正确调用它,但是,当我分析结果时var_dump($loop) 我看到帖子甚至没有被查询:

object(WP_Query)#9671 (49) {
  ["query"]=>
  array(3) {
    ["post_type"]=>
    string(8) "projects"
    ["post__in "]=>
    array(3) {
      [0]=>
      int(79)
      [1]=>
      int(98)
      [2]=>
      int(108)
    }
    ["posts_per_page"]=>
    int(3)
  }
  ["query_vars"]=>
  array(65) {
    ["post_type"]=>
    string(8) "projects"
    ["post__in "]=>
    array(3) {
      [0]=>
      int(79)
      [1]=>
      int(98)
      [2]=>
      int(108)
    }
    ["posts_per_page"]=>
    int(3)
    ...
  ["request"]=>
  string(276) "SELECT SQL_CALC_FOUND_ROWS  wpKX_posts.ID FROM wpKX_posts  WHERE 1=1  AND wpKX_posts.post_type = \'projects\' AND (wpKX_posts.post_status = \'publish\' OR wpKX_posts.post_status = \'acf-disabled\' OR wpKX_posts.post_status = \'private\')  ORDER BY wpKX_posts.post_date DESC LIMIT 0, 3"
  ["posts"]=>
  array(3) {
    [0]=>
    object(WP_Post)#9663 (24) {
      ["ID"]=>
      int(110)
      ...
    }
    [1]=>
    object(WP_Post)#9603 (24) {
      ["ID"]=>
      int(108)
      ...
    }
    [2]=>
    object(WP_Post)#9602 (24) {
      ["ID"]=>
      int(111)
      ...
    }
  }
  ["post_count"]=>
  int(3)
我做错了什么?

3 个回复
SO网友:Sally CJ

(Revised to improve the wording)

Actually, you do not have to set the orderby to post__in for post__in query to work. There\'s no such limitation in WordPress and you\'d only need to set \'orderby\' => \'post__in\' when you want to sort the posts exactly in the same order as the post IDs in the post__in parameter. :)

And the actual problem in your code, is that WordPress (or the WP_Query class) doesn\'t recognize your post__in parameter because there is a trailing space inside the parameter name — PHP doesn\'t automatically remove trailing spaces in array keys or even values, and so does WordPress (i.e. the spaces are preserved); so $args[\'post__in \'] (note the space) and $args[\'post__in\'] are two different array items:

$args = array( \'post_type\' => \'projects\', \'post__in \' => $projectIDs, \'posts_per_page\' => 3 );

And even in the var_dump() output, you could quite clearly see the trailing space here:

["post__in "]=>

So the fix is simple: remove the space. And try again with your query, where you should see it now has the proper IN clause:

AND wpKX_posts.ID IN (79,98,108)

And actually, you don\'t need to manually add the indexes here:

//$projectIDs = array( [0] => 79, [1] => 98, [2] => 108 );
$projectIDs = array( 79, 98, 108 ); // no manual indexes; still would work fine
SO网友:user2695006

如果您对此仍有问题,可以尝试以下方法:

https://developer.wordpress.org/reference/classes/wp_query/

post\\uu in(数组)–使用post ID。指定要检索的帖子。注意:如果您使用粘性贴子,则会将其包括在内(带前缀!)在您检索的帖子中,无论您是否想要它。要抑制此行为,请使用ignore_sticky_posts.

$args = [ 
\'post_type\' => \'projects\', 
\'post__in \' => $projectIDs, 
\'posts_per_page\' => 3, 
\'ignore_sticky_posts\' => true 
];

SO网友:Tanmay Patel

在Wordpress 3.5及以上版本中,您可以使用“orderby”=>“post\\uu In”,然后您必须编写以下内容:

<?php
$projectIDs = array(79, 98, 108);
$args = array( \'post_type\' => \'projects\', \'post__in \' => $projectIDs, \'posts_per_page\' => 3, \'orderby\'=>\'post__in\' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
endwhile;
?>

相关推荐

The Loop not looping?

在进行了大量的故障排除和搜索之后,我想我终于明白了如何进行循环。但我似乎无法让它真正循环!我创建了一个子主题,并添加了functions.php 文件中包含以下代码:<?php function my_theme_enqueue_styles() { $parent_style = \'grow-thinkup-style-minimal\'; // This is \'twentyfifteen-style\' for the Twenty Fifteen the