WP查询并按特定指定ID排序

时间:2016-03-07 作者:JackTheKnife

我的WP_Query 请求如下所示:

$query_args = array(\'posts_per_page\' => $products, \'no_found_rows\' => 1, \'post_status\' => \'publish\', \'post_type\' => \'product\' );
$query_args[\'meta_query\'] = $woocommerce->query->get_meta_query();
$query_args[\'meta_query\'][] = array(
    \'key\' => \'_featured\',
    \'value\' => \'yes\'
);

$r = new WP_Query($query_args);
我需要添加什么参数来返回按特定行中的ID排序的查询(例如,我需要按该顺序返回产品ID 161165131202)。

编辑:我添加了post__inorderby 参数,该参数仅按ID提取那些特定产品,但不按数组中指定的顺序提取,但看起来是基于产品添加到后端的时间。

$query_args = array(\'posts_per_page\' => $products, \'no_found_rows\' => 1, \'post_status\' => \'publish\', \'post_type\' => \'product\', \'post__in\' => array(161,165,131,202), \'orderby\' => \'post__in\', \'order\' => \'ASC\' );
WP查询如下所示:

SELECT wp_posts.ID 
FROM wp_posts  
INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )  
INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) 
WHERE 1=1  
AND wp_posts.ID IN (161,165,131,202) 
AND wp_posts.post_type = \'product\' 
AND ((wp_posts.post_status = \'publish\')) 
AND ( ( wp_postmeta.meta_key = \'_visibility\' 
AND CAST(wp_postmeta.meta_value AS CHAR) IN (\'visible\',\'catalog\') ) 
AND (mt1.meta_key = \'_featured\' AND CAST(mt1.meta_value AS CHAR) = \'yes\' )) 
GROUP BY wp_posts.ID 
ORDER BY wp_posts.menu_order, FIELD( wp_posts.ID, 161,165,131,202 ) 
LIMIT 0, 5
不知道为什么ORDER BY 设置为menu_orderpost__in

3 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

保持传递给post__in 参数,您需要传递post__in 作为对的值orderby 参数和ASCorder 论点

$query_args = [
    \'posts_per_page\' => $products, 
    \'no_found_rows\'  => true, 
    \'post_type\'      => \'product\', 
    \'post__in\'       => [161,165,131,202],
    \'orderby\'        => \'post__in\',
    \'order\'          => \'ASC\'
];
编辑查看您的编辑,您的某个地方的过滤器不好,很可能是坏的pre_get_posts 动作或aposts_orderby 滤器进行以下两项测试

添加\'suppress_filters\' => true, 对于查询参数,如果这有帮助的话,您的筛选器不好

添加remove_all_actions( \'pre_get_posts\' ); 在您进行查询之前,如果这有帮助的话,您有一个坏的pre_get_posts 滤器

三分之一,但最有可能的无用支票是wp_reset_query() 在自定义查询之前,如果有帮助的话,您的某个地方有一个错误的查询,很可能是使用query_posts

SO网友:juz

要包含不在数组中的其他帖子,可以尝试以下操作,创建一个带有键的数组,该键将帖子id与指示符组合在一起,这样就可以进行键排序(ksort),并首先返回带有该数组的所有帖子

$query_args = array(\'posts_per_page\' => $products, \'no_found_rows\' => 1, \'post_status\' => \'publish\', \'post_type\' => \'product\' );

$r = new WP_Query($query_args);

$my_posts = array();
$id_order = array(161, 165, 131, 202);

foreach($r->posts as $key => $the_post){
  if (in_array($the_post->ID, $id_order)) {
    $index = array_search($the_post->ID, $id_order);
    $my_posts["a" . $index . \'_\' . $the_post->ID] = $the_post; // store the order of the returned posts by post ID
  } else {
    $my_posts["b0_" . $the_post->ID] = $the_post; // store the order of the returned posts by post ID
  }
}

ksort($my_posts);

foreach($my_posts as $post){ // go through custom list of post IDs
  setup_postdata($post);

  // do your content here
  ?><article><h2><?php the_title();?></h2><?php
  the_content();
  ?></article><?php
}

SO网友:Tim Malone

您可以按升序或降序排序,但不能指定自定义顺序-至少在查询参数中。

要完成您想要做的事情(按特定顺序显示特色产品),最简单的方法是在查询发生后重新排列结果。也就是说,如果您希望更改订单(或添加新产品)时,可以回来更改此代码。如果您想要一个更健壮的解决方案,我会考虑在产品中添加一个自定义字段,例如“featured\\u order”,然后按该字段对查询排序。如果你愿意的话,我可以把它念一遍。

也就是说,假设您正在寻找最简单的解决方案,那么您将希望在发布的代码段末尾添加类似的内容:

$my_posts = array();
$id_order = array(161, 165, 131, 202);

foreach($r->posts as $key => $the_post){
  $my_posts[$the_post->ID] = $key; // store the order of the returned posts by post ID
}

foreach($id_order as $id){ // go through custom list of post IDs
  $post = $r->posts[$my_posts[$id]]; // grab the query result for the post ID we want
  setup_postdata($post);
  // the_title(); the_content(); ........
}
需要注意的是,如果post ID不在$id_order 数组,则此代码不会显示它-即使它是由查询返回的。

EDIT: 如果要确保数组中没有的其他帖子仍然返回,请参阅juz发布的答案。