Order get_terms using a Custom Field

Rather than outputting your terms in that initial loop, I would use it instead to build a new array, with your issue_date as the key: $my_new_array = array( ); foreach ( $terms as $term ) { $issue_date = get_field( ‘issue_date’, $term ); $my_new_array[$issue_date] = $term->name; } You can then loop through this new array in … Read more

Order by DESC, ASC in custom WP_Query

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 … Read more

Sorting: custom query with orderby meta_value_num THEN by title

This is very crude but should sort your posts by year (meta_value) and then by title. It does depend on how the query is setup so it will only work with the query below or with similar ones. function alter_order_wpse_103181($order,$qry) { remove_filter(‘posts_orderby’,’alter_order’,1,2); $order = explode(‘,’,$order); $order = implode( ‘ ASC,’,$order); return $order; } add_filter(‘posts_orderby’,’alter_order_wpse_103181′,1,2); $q … Read more

multiple orderby in pre_get_posts action

As Milo said : $query->set(‘meta_key’, ‘wpcf-object-sold-status’ ); $query->set(‘orderby’, array(‘meta_value’ => ‘ASC’, ‘date’ => ‘DESC’)); // $query->set(‘order’, ‘ASC DESC’ ); // not needed Relevant link: https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/

Get current menu_order

If you have the post with an $id: $thispost = get_post($id); $menu_order = $thispost->menu_order; WordPress itself does not provide a function to get the menu_order, so you have to query the post-Object. If you are outside the loop, you can use the above function, however inside the loop you could also achieve this by: global … Read more

WP_Query orderby date not working

This will definitely work….It worked for me… $username = get_the_author_meta( ‘login’, $author_id ); $args = array( ‘post_type’ => ‘any’, ‘orderby’ => ‘date’, ‘order’ => ‘DESC’, ‘suppress_filters’ => true, ‘tax_query’ => array( array( ‘taxonomy’ => ‘author’, ‘field’ => ‘name’, ‘terms’ => $username ) ) ); $query = new WP_Query( $args );