wp_query post__not_in 不起作用

Âng*_*elo 2 mysql wordpress

我正在尝试使用 wp_query 构建查询,其中必须排除某些帖子 ID。这是我正在使用的代码:

$args = array( 'posttype' => 'post', 'posts_per_page' => 1, 'cat' => 14, 'post__not_in ' => array(71,1), 'orderby' => 'menu_order', 'order' => 'ASC' , 'post_status' => 'publish' );
$the_query = new \WP_Query( $args );
Run Code Online (Sandbox Code Playgroud)

但是 wp_query 仍然返回带有这些 ID 的帖子。

这是带有 wp_query 对象的 pastebin http://pastebin.com/gjayN4Yc。至于 wp_query->request 我有以下几点:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (14,15) ) AND wp_posts.post_type = 'post' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order ASC LIMIT 0, 3
Run Code Online (Sandbox Code Playgroud)

我做错了什么还是这是一个核心错误?

谢谢你。

hel*_*nya 7

我运行了你的代码,它对我来说很好用,除了'post__not_in '. 你看到了吗?确保您拥有密钥为'post__not_in'

另外,有几点需要说明:

  1. 它是'post_type'和不是'posttype
  2. 'menu_order'不适用于帖子。这是为分层帖子类型分配的,例如pages. 您在页面顺序 UI 中设置该字段,该字段(默认情况下)不适用于帖子。

这是修改后的代码:

$args  = array(
    'post_type'      => 'post',
    'posts_per_page' => 1,
    'cat'            => 14,
    'post__not_in'   => array( 71, 1 ),
    'orderby'        => 'date',
    'order'          => 'ASC',
    'post_status'    => 'publish',
);
$the_query = new WP_Query( $args );
if ( ! $the_query->have_posts() ) {
    // maybe echo a message that none were found
    return;
}

while( $the_query->have_posts() ) {
    $the_query->the_post();

    // do your business logic here

    // then call the view file to render the HTML
}
wp_reset_postdata();
Run Code Online (Sandbox Code Playgroud)

然后 SQL 查询变为:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID 
FROM wp_posts  
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) 
WHERE 1=1  AND wp_posts.ID NOT IN (71,1) AND 
           ( wp_term_relationships.term_taxonomy_id IN (14) ) AND 
            wp_posts.post_type = 'post' AND 
            ((wp_posts.post_status = 'publish'))
GROUP BY wp_posts.ID 
ORDER BY wp_posts.post_date ASC LIMIT 0, 1
Run Code Online (Sandbox Code Playgroud)