Hot answers tagged

34

Set orderby to post__in. This preserves post ID order given in the post__in array (available with Version 3.5). $args = array ( 'posts_per_page' => -1, 'post__in' => $ids, 'orderby' => 'post__in' );


19

Easy Peasy, just tested 2018, using in production currently. $query->set( 'meta_query', array( 'relation' => 'OR', array( 'key' => 'custom_meta_key', 'compare' => 'EXISTS' ), array( 'key' => 'custom_meta_key', 'compare' => 'NOT EXISTS' ) ) ); $query->set( 'orderby', 'meta_value title'...


17

You are using meta query without setting a value. The way you are doing it is using to query posts, not to order them. Using Named Meta Queries To order your posts by different meta datas, you can give your meta queries a name and then use that to set the ordering. Here is a simple example for you: $args = array( 'meta_query' => array( '...


16

You can try following code: get_post_field( 'menu_order', $post_id);


16

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/


14

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( '...


13

You can create groups of meta_queries using specific compare operation on them, and since you want to order based in a single custom field, you can keep the order declaration dedicated to the single meta field. So: $q = new WP_Query( array( 'meta_key' => 'population', //setting the meta_key which will be used to order 'orderby' => ...


12

An easier way may be to go through and change the permalink slug on those posts that need it (under the title on the post writing screen) and then just use that for ordering instead of the title. ie. use post_name not post_title for sorting... This would also mean that your permalink may be different if you use %postname% in your permalink structure, ...


12

Try this... $posts = get_posts( array( "orderby"=> "slug", "order" => "ASC", "post_type" => "my-custom-post-type", "posts_per_page" => -1, "fields" => "ids", "meta_query" => array( array( "key" => "ams_park_id", "value" => get_the_ID(), ...


11

'meta_value_num' is not magic, it casts a value as numeric, however, to be properly cast as numeric, a value must be numeric compatible. In OP it's said that the field values are "numeric" but I bet there's something not numeric, e.g. a currency symbol like "10 $" or "$ 10.00", or other symbol like "10-". Also consider that using a thousands separator most ...


11

This method will return all of the posts including those with and without the requested meta_key, but it will do weird things when ordering. add_action('pre_get_posts', 'my_stuff'); function my_stuff ($qry) { $qry->set( 'meta_query', array( 'relation' => 'OR', # Matches to this meta_query should be added to those ...


10

You can pass an array to the query as the following example described in the Codex shows: $args = array( 'orderby' => array( 'title' => 'DESC', 'menu_order' => 'ASC' ) ); $query = new WP_Query( $args ); In your case will be something like this: /* Order Posts Alphabetically */ function prefix_modify_query_order( $query ) { if ( is_main_query()...


8

SOLUTION - Updated: 03/03/2015 - Thanks to /u/G.M. The below adds a new field to the Term Edit Page and saves the value into the un-used ( for the moment anyway ) term_group field which can then be used to order terms. In the below hooks ( actions ) you'll need to replace TAXONOMY_SLUG with your actual taxonomy slug. /** * Append Fields To Term Edit Page ...


8

Post list in admin (edit.php) use a normal WP_Query, just like frontend can be changed using pre_get_posts. add_action('pre_get_posts', 'reorder_my_cpt'); function reorder_my_cpt( $q ) { if ( !is_admin() || !$q->is_main_query() ) { return; } $s = get_current_screen(); // change 'book' with your real CPT name if ( $s->base === 'edit' &...


8

A better approach could be using three queries. First two query retrieve the post ids, and third one query post by ids. // first query $first_ids = get_posts( array( 'fields' => 'ids', 'posts_per_page' => '10', 'post_status' => 'publish', 'post_type' => array('news','partners'), 'orderby' => 'date', ...


8

Try this: function my_custom_ordering( $query ) { if($query->is_main_query() AND !is_admin() ) { if ( $query->is_home() || $query->is_category() || $query->is_tag() ){ $query->set( 'orderby', 'modified' ); } } } add_action( 'pre_get_posts', 'my_custom_ordering' ); This will set orderby field to modified in ...


8

A much shorter solution, just add this before foreach: usort($terms, function($a, $b) { return get_field('issue_date', $a) - get_field('issue_date', $b); });


8

The Problem I think there's a typo in there: The name of the filter is posts_fields not post_fields. That could explain why the title2 field is unknown, because it's definition isn't added to the generated SQL string. Alternative - Single filter We can rewrite it to use only a single filter: add_filter( 'posts_orderby', function( $orderby, \WP_Query $q ...


7

The problem that everyone here is having has to do with the order of the meta queries. In order to sort correctly, you will need to put the "NOT EXISTS" query before the "EXISTS" query. The reason for this is because WordPress uses the meta_value of the last "LEFT JOIN" statement in the "ORDER BY" clause. For example: $pageQuery = new WP_Query([ '...


7

First, you need to store the actual login date, because this is not stored by default. You can use this code to do that(use it in your functions.php) add_action('wp_login','user_last_login', 0, 2); function user_last_login($login, $user) { $user = get_user_by('login',$login); $now = time(); update_usermeta( $user->ID, 'user_last_login', $...


7

Completely edited after first publish The problem is that to order for a meta value, WordPress need to 'meta_key' in query is set to something. But if you set 'meta_key' to something, then WordPress will add something like AND ( wp_postmeta.meta_key = 'the_meta_key' ) to WHERE SQL clause; and something like INNER JOIN wp_postmeta ON ( wp_posts.ID = ...


7

One way to do it is to store the order of the tags as post meta data, using the action set_object_terms which happens to pass the tags in the order they appear on edit, eg in your "functions.php": // Called in admin on updating terms - update our order meta. add_action( 'set_object_terms', function ( $object_id, $terms, $tt_ids, $taxonomy, $append, $...


7

The main query is generated before the template is loaded, the results of the main query are how WordPress knows what template to load. If you want to alter query parameters of the main query to change things like orderby, you should add a function hooked to pre_get_posts. The argument passed to the function contains the query parameters of each query, so ...


6

You just need get current date and add it on data_query in wp_query, Look this: <?php $getdate = getdate(); $args = array( 'date_query' => array( array( 'year' => $getdate["year"] ), ), ); $query = new WP_Query( $args ); ?> and then use loop: <?php if ( $query->have_posts() ): while ( $query->...


6

I realize this is an old thread, but I came across it trying to do the same thing and settled on adding a pre-query action using functions.php rather than a template and the loop. In my case; I have a page of about 25 yoga classes, set up as a custom post type 'classes' // function and action to order classes alphabetically function alpha_order_classes( $...


6

The original code is not so bad. It just is missing the meta_key parameter. Example: $theusers = get_users( array( 'role' => 'contributor', 'meta_key' => 'first_name', 'orderby' => 'meta_value', ) );


6

You don't declare the order of the results when you register the post type. Instead, you do at the time the results are requested. If you're using WP_Query to get the results then you add your orderby and order arguments to the request. http://codex.wordpress.org/Class_Reference/WP_Query If you want to change the order of the results on the archive pages ...


6

EDIT I have improved the code a bit. All code block are updated accordingly. Just a note though before jumping into the updates in the ORIGINAL ANSWER, I have set up the code to work with the following Custom post type -> release Custom taxonomy -> game Make sure to set this according to your needs ORIGINAL ANSWER In addition to the other answers and ...


6

You could try this: // Get posts terms $terms = wp_get_post_terms( $post->ID, 'winetype', array( 'order' => 'DESC') ); $sep = ', '; foreach ( $terms as $term ) { echo $term->name; echo $sep; }


5

First, I would solve your problem using WPQuery. Best to go there as opposed to query_posts Second, once you acknowledge and accept that WP Query is your friend for as long as you are within the wonderful world of WP theming, then you have to do as follows to solve your particular pickle: if(is_category('your_category') : $args = array( '...


Only top voted, non community-wiki answers of a minimum length are eligible