Hot answers tagged

29

When declaring your custom post type using the register_post_type function, you have to add 'page-attributes' to the support field, like in the following example: register_post_type('myposttype', array( 'supports' => array('title', 'editor', 'page-attributes'), 'hierarchical' => false )); You'll need to add any other supported meta boxes as ...


24

$args = array( 'post_type' => 'testimonials', 'posts_per_page' => 4, 'orderby' => 'post__in', 'post__in' => array(883, 563, 568, 106) ); Using post__in within the orderby value it will honour the order of the array of IDs passed in post__in


24

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

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


17

If you don't wish to always click the "Title" column to sort your posts by title, you can place this code in either your currently active WordPress theme's functions.php file, or within a plugin. This will automatically always sort your posts for you, so you don't have to click the title column every time. You can use this for setting default sort order on ...


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

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 $post; $menu_order = $post-...


16

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


15

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


14

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/


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


10

It's been too long, but just for the record, you can display the 'menu order' option in the admin, just by including 'page-attributes' in the 'supports' option array. For example: register_post_type( 'columna', array( 'labels' => array( 'name' => __( 'Columnas' ), 'singular_name' => __( 'Columna' ), ),...


10

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

Actually, with a little digging this is quite easy to do. WooCommerce has already set up a filter so all you have to do it add a small snippet in your functions.php file. // Modify the default WooCommerce orderby dropdown // // Options: menu_order, popularity, rating, date, price, price-desc function my_woocommerce_catalog_orderby( $orderby ) { unset($...


10

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


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

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


8

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


7

Ah, click that little title thingy to toggle alphabetical sorting....


7

Almost the same approach One Trick Pony has chosen, but I am using translated names and uasort() (to preserve the keys): add_filter( 'editable_roles', 't5_sort_editable_roles' ); /** * Array of roles. * * @wp-hook editable_roles * @param array $roles * @return array */ function t5_sort_editable_roles( $roles ) { uasort( $roles, '...


7

Why don't use built-in functionality of PHP? Put the following line right before the foreach: usort($members, create_function('$a, $b', 'return strnatcasecmp($a->last_name, $b->last_name);')); References: usort create_function strnatcasecmp


7

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 ) { $s = get_current_screen(); // change 'book' with your real CPT name if ( is_admin() && $s->base === 'edit' && $s->post_type === 'book' &&...


7

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


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

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', $...


6

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


6

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 = ...


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