How to sort the result of wp_query How to sort the result of wp_query wordpress wordpress

How to sort the result of wp_query


Look at the orderby and order parameters in WP_Query. If you need to sort by post title, you can add the following to your query parameters

'orderby' => 'title''order' => 'ASC'

EDIT

If you need to sort with usort, you can do the following

usort($the_query->posts, function($a, $b) {   return strcasecmp(                 $a->post_title,                 $b->post_title             );});


WP_Query('orderby=date&order=DESC')

Or try this, if you want to sort based on custom meta value.

$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;}