pendo
pendo

Reputation: 840

WP_Query ordering by menu_order and not title

I have a custom post type called "customer". It has attributes of "customer_currentpast" that can be 'current' or 'past'. The customers are related to a partner. The partnerid is passed in as well. I want to sort by the customer name/title.

The query does not come back sorted by title, but rather by menu_order and then title. It should NOT be sorting by menu_order. I do a query reset immediately before the WP_Query and then the dump right after and the results are sorted by menu_order, which is wrong. Should be title.

<?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
?>

This is the query this outputs

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

Upvotes: 0

Views: 3508

Answers (1)

Akshay Shah
Akshay Shah

Reputation: 3504

Put this code in your functions.php file

function filter_query( $query ) {
 $query = str_replace("tableprefix_posts.menu_order,", "", $query);
 return $query;
}

We can add argument in our query ignore_custom_sort = true

So in your case put the below argument.

 $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 )

If this is not gone to work for you than please share the plugins which you have used and give me the list of them.

Upvotes: 0

Related Questions