9

I need to do multi-level ordering in a query. The issue is with ordering one value DESC and the other ASC as in SQL. The following SQL seems to give me what I want when I run it in terminal:

SELECT DISTINCT * FROM wp_posts 
INNER JOIN wp_postmeta 
ON wp_posts.ID = wp_postmeta.post_id 
WHERE wp_posts.post_type = 'post' 
AND wp_postmeta.meta_key = 'pb_issue_featured'
AND wp_posts.post_status = 'publish' 
ORDER BY wp_postmeta.meta_value DESC, wp_posts.menu_order ASC;

The pb_issue_featured is a boolean value. The end result I need is the query to display posts that have a meta value of 1 for this field at the top, then all the others bellow. Then the second tier ordering is the designated menu_order (I'm using the post types order plugin).

The issue is that my boolean value needs to be ordered high to low (1 to 0) but the menu_order is the opposite. What is ordered first with the plugin has a menu order of 1. So using the built in 'orderby' in WP_Query doesn't work. Anyone have suggestions? I looked into the 'posts_orderby' filter but couldn't get it to take. Wasn't really sure where it should be applied or how I could troubleshoot it. It just didn't re order the way I had it.

Thanks for the help! I will post the actual WP_Query if it's relevant but I wanted to keep this as short as possible.

The query args:

$args = array(
        'post_type' => 'post',
        'meta_key' => 'pb_issue_featured',
        'orderby'   => 'meta_value',
        'order' => 'DESC',
        'post_status' => 'publish',
        'posts_per_page' => $posts,
        'paged' => $paged,
        'meta_query' => array(
            array(
                'key' => 'headline',
                'value' => 1,
                'compare' => '!=' 
                )
            )
        );
$q = new WP_Query($args);
2
  • Well, you should post the WP_Query, to see if it is formed right. Aug 13, 2013 at 5:53
  • There it is in all it's glory. I'm currently not ordering by anything but that boolean meta value b/c it was the only way to get that level or sorting working. The site also uses 'headline' posts which take precedence over 'featured' posts. Thus I exclude them in this query
    – ian
    Aug 13, 2013 at 6:02

3 Answers 3

27

Try this:

$args = array(
        'post_type' => 'post',
        'meta_key' => 'pb_issue_featured',
        'orderby'   => 'meta_value',
        'order' => 'DESC',
        'posts_per_page' => $posts,
        'paged' => $paged,
        'paged' => 1,
        'meta_query' => array(
            array(
                'key' => 'headline',
                'value' => 1,
                'compare' => '!=' 
                )
            )
        );

add_filter( 'posts_orderby', 'filter_query' );
$q = new WP_Query($args);
remove_filter( 'posts_orderby', 'filter_query' );

function filter_query( $query ) {
    $query .= ', wp_posts.menu_order ASC';
    return $query;
}
2
  • Like a boss Spartakus. Thanks for the concise and accurate answer. This worked like a charm
    – ian
    Aug 13, 2013 at 7:18
  • 1
    You could also use an array in the order by clause (WP 4.0 and newer)
    – Blueriver
    Apr 25, 2017 at 13:55
3

ORDER BY ID ASC

$args = array(
    'order' => 'ASC',
    'orderby' => 'ID',
);

or

ORDER BY ID DESC

$args = array(
    'order' => 'DESC',
    'orderby' => 'ID',
);
1
  • I don't think that solves the original problem in the question which was to order by two different properties in two different directions.
    – Rup
    Feb 2, 2021 at 9:21
0

when using something like elementor plugin and have a custom field with date name, add the following action to elementor-pro/elementor-pro.php in plugin editor.

add_action( 'elementor/query/my_custom_filter', function( $query ) {
 
    $query->set( 'meta_key', 'date' );
    $query->set( 'orderby', 'meta_value' );
    $query->set( 'order', 'DESC' );
    $query->set( 'posts_per_page', 2 );     
    
} );
2
  • How does Elementor come into play here? There's literally no mention of Elementor in the OP's question or any subsequent answers or comments. Dec 30, 2020 at 0:01
  • @TonyDjukic, you are pretty right with Elementor Irrelevancy. But because google showed me this page after searching for my specific error about customizing WP_Query and it took some time to find the solution, I thought that, it would help somebody else with the same problem.
    – Iman
    Jan 7, 2021 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.