我有一个名为"客户"的自定义帖子类型。 它具有"customer_currentpast"的属性,可以是"当前"或"过去"。 客户与合作伙伴相关。 合作伙伴 ID 也会传入。 我想按客户名称/职务排序。
查询不会按标题排序,而是按menu_order然后按标题排序。 它不应该按menu_order排序。 我在WP_Query之前立即执行查询重置,然后在之后立即进行转储,结果按menu_order排序,这是错误的。 应该是标题。
<?php
wp_reset_query();
$customersCurrentQuery = new WP_Query( array(
'post_type' => 'customer',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'and',
array(
'key' => 'customer_currentpast',
'value' => 'current',
'compare' => '=',
),
array(
'key' => 'partnerid',
'value' => $post->ID,
'type' => 'numeric',
'compare' => '=',
),
) //meta_query
) //args
); //wp_query
?>
这是此输出的查询
SELECT tableprefix_posts.*
FROM tableprefix_posts
INNER JOIN tableprefix_postmeta ON ( tableprefix_posts.ID = tableprefix_postmeta.post_id )
INNER JOIN tableprefix_postmeta AS mt1 ON ( tableprefix_posts.ID = mt1.post_id )
WHERE 1=1 AND (
( tableprefix_postmeta.meta_key = 'customer_currentpast' AND tableprefix_postmeta.meta_value = 'current' )
AND
( mt1.meta_key = 'partnerid' AND CAST(mt1.meta_value AS SIGNED) = '43' )
) AND tableprefix_posts.post_type = 'customer'
AND (tableprefix_posts.post_status = 'publish' OR tableprefix_posts.post_status = 'private')
GROUP BY tableprefix_posts.ID
ORDER BY tableprefix_posts.menu_order, tableprefix_posts.post_title ASC
把这段代码放在你的函数.php文件中
function filter_query( $query ) {
$query = str_replace("tableprefix_posts.menu_order,", "", $query);
return $query;
}
我们可以在查询中添加参数ignore_custom_sort = true
所以在你的情况下,提出以下论点。
$customersCurrentQuery = new WP_Query( array(
'post_type' => 'customer',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'and',
array(
'key' => 'customer_currentpast',
'value' => 'current',
'compare' => '=',
),
array(
'key' => 'partnerid',
'value' => $post->ID,
'type' => 'numeric',
'compare' => '=',
),
) //meta_query
) //args
); //wp_query
$query = new WP_Query( $customersCurrentQuery );
add_filter( 'posts_orderby', 'filter_query',99,1 )
如果这对您不起作用,请分享您使用过的插件并给我它们的列表。