我的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__in
和orderby
参数,该参数仅按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_order
不
post__in
最合适的回答,由SO网友:Pieter Goosen 整理而成
保持传递给post__in
参数,您需要传递post__in
作为对的值orderby
参数和ASC
到order
论点
$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
动作或a
posts_orderby
滤器进行以下两项测试
添加\'suppress_filters\' => true,
对于查询参数,如果这有帮助的话,您的筛选器不好
添加remove_all_actions( \'pre_get_posts\' );
在您进行查询之前,如果这有帮助的话,您有一个坏的pre_get_posts
滤器
三分之一,但最有可能的无用支票是wp_reset_query()
在自定义查询之前,如果有帮助的话,您的某个地方有一个错误的查询,很可能是使用query_posts