WP_Query按menu_order排序,而不是标题

时间:2018-05-30 22:39:04

标签: php mysql wordpress

我有一个名为“customer”的自定义帖子类型。它具有“customer_currentpast”属性,可以是“当前”或“过去”。客户与合作伙伴有关。 partnerid也被传入。我想按客户名称/标题排序。

查询不按标题排序,而是按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

1 个答案:

答案 0 :(得分:0)

将此代码放在functions.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 )

如果这不适合你,请分享您使用过的插件,并将其列出给我。