Skip to main content
39 votes

Make custom column sortable

Make sure to change MY_POST_TYPE, MY_CUSTOM_COLUMN and MY_META_KEY to the actual values. First, add your custom column. Unset the date and set it again to keep it in the last column. You can skip ...
Max Yudin's user avatar
  • 6,328
9 votes

Can the Next/Prev Post links be ordered by menu order or by a meta key?

Tried to hook in without success. Might be just a problem of my configuration, but for those who can't make the hook work, here is the simplest solution: <?php $all_posts = new WP_Query(array( ...
Szabolcs Páll's user avatar
7 votes
Accepted

Random sort within an already sorted query

There is no need to do an overrated amount of queries to accomplish this. you can still use only one query, the_posts filter and some PHP to sort your code as needed. I take that this is a custom ...
Pieter Goosen's user avatar
7 votes
Accepted

How to Arrange Posts by Size in the WordPress Dashboard?

Sorting by size Adding custom column and sorting by post_content length is possible. It won't be very efficient though, I'm afraid. First you'll have to add your custom column and display the length ...
Krzysiek Dróżdż's user avatar
6 votes
Accepted

How to Group Posts by the First Letter or Number?

I think there's a better approach, just create a custom taxonomy that holds the alphanumeric terms, then assign each post to the correct term. You can use the save post action to auto assign posts to ...
Alex Sancho's user avatar
6 votes
Accepted

Sort in WP_Query(), not filter? Is it possible?

If you want to sort the posts by the meta post_views_count, and still include posts that do not have that meta, you can use meta_query like so: 'meta_query' => array( 'relation' => 'OR', // ...
Sally CJ's user avatar
  • 39.4k
5 votes

register_post_type sort order by title by default

I found this answer was better here (altered to suit question): function se91124_order_post_type($query) { if($query->is_admin) { if ($query->get('post_type') == 'States') { ...
hazrpg's user avatar
  • 225
5 votes
Accepted

Sorting search results by taxonomy terms

Unfortunately, although WP_Query supports the 'tax_query' arg, it does not support ordering based on post terms. So you will need to modify the query SQL, as you are doing now. However, you are ...
J.D.'s user avatar
  • 3,915
5 votes

Filter admin columns by custom post field value

For comparison, I made a post type of dog with both a custom field dog_colour and a taxonomy dog_colour. With both added as admin columns, we get: The taxonomy terms are automatically linked with a ...
Andy Macaulay-Brook's user avatar
4 votes

Can the Next/Prev Post links be ordered by menu order or by a meta key?

FWIW here’s how you can order by menu_order for a specific custom post type: /** * Customize adjacent post order */ add_filter('get_next_post_sort', function($order) { if (is_singular('...
Ted Whitehead's user avatar
4 votes

Orderby meta_value only returns posts that have existing meta_key

I tried applying @Manny Fleurmond's answer and like @Jake I couldn't get it to work even after correcting the typo that 'orderby' => 'meta_key' should be 'orderby' => 'meta_value'. (And for ...
jlad26's user avatar
  • 51
4 votes
Accepted

Orderby Post Views

You can sort your query by view number, using the meta key : $query = new WP_Query( array( 'meta_key' => 'post_views_count', 'orderby' => '...
Johansson's user avatar
  • 15.3k
4 votes
Accepted

how to get post order by post id wp_query?

OK, so you want to define posts order by yourself. WP_Query allows you to do that - you'll have to use orderby => post__in to achieve it. And that's what you do. So why isn't it working? Because of a ...
Krzysiek Dróżdż's user avatar
4 votes

Filter admin columns by custom post field value

Based on @ Andy Macaulay-Brook comment on your question you can try this (it will add a dropdown next to the Filter button on top of the admin listing table): add_action( 'restrict_manage_posts', '...
Qaisar Feroz's user avatar
  • 2,137
4 votes
Accepted

Display sorting options dropdown when using WooCommerce product category shortcode

Since WooCommerce 3.2, Woocommerce shortcodes and their available attributes have changed. So try the following shortcode instead (for "foo" product category): [product_category limit="90" columns="...
LoicTheAztec's user avatar
  • 3,361
4 votes

Display sorting options dropdown when using WooCommerce product category shortcode

If you would rather want to get the sorting menu using a shortcode then you can create one for it in your functions.php // Creating Shortcode for Product Sorting add_shortcode('wc_sorting','...
Abhishek's user avatar
3 votes
Accepted

Number of pages - multiple (custom) post types

Milo is right, you should be using pre_get_posts instead of calling a 2nd query on load. This will make paging much easier. Whatever the orderby session you're using it should still be accessible in ...
Howdy_McGee's user avatar
  • 20.7k
3 votes

Sorting Grids with Essential Grid and Events Manger

After researching this quite a bit I managed to sort the Events Manager events by date in the Essential Grid plugin. If you started with the solution above, you can delete the added parameters field ...
Marc's user avatar
  • 33
3 votes

Query Set Order By Author

To order by the authors nicename is not nativly supported by the WP_Query. If you order by author, as you have experienced, you order by the ID of the author. So, you would need to actually go into ...
websupporter's user avatar
  • 2,989
3 votes

Can the Next/Prev Post links be ordered by menu order or by a meta key?

Based on @Szabolcs Páll's answer I've created this utility class with helper methods to be able to get posts of type by menu order and get the next and previous post by menu order as well. I've ...
Eli Jayson's user avatar
3 votes
Accepted

How to sort posts in admin by titles with dd.mm.yyyy format?

It looks like the post titles have the date format DD.MM.YYYY. We can order the DD.MM.YYYY titles as YYYYMMDD with: $query = new WP_Query( [ 'orderby' => 'wpse_mod_title ' ] ); that's supported ...
birgire's user avatar
  • 67.5k
3 votes

list posts and wrap them alphabetically A-Z

I would do it the other way around- loop over the alphabet and check all posts for a match with each letter. $all_posts = new WP_Query( array( 'orderby' => 'title', 'order' =&...
Milo's user avatar
  • 78.6k
2 votes

Can the Next/Prev Post links be ordered by menu order or by a meta key?

Based on @Szabolcs Páll's answer and bbloomer's post on adding next/prev buttons in WooCommerce Single Product Page, I created this code. It sorts all products by meta key and adding prev/next ...
Elron's user avatar
  • 159
2 votes

how to sort post in admin column by recently

add_action( 'pre_get_posts', 'change_post_sort', 1 ); function change_post_sort( $query ) { if ( is_admin() && $query->is_main_query() ) { $query->set( 'order' , '...
MohamadHossein's user avatar
2 votes

How to show the entries in a custom taxonomy by author?

Well, when you need to modify a theme you create a "child theme." There are good instructions for how here. So we would just create a safe copy of the modified code in a folder the other theme doesn'...
adamlaughlin's user avatar
2 votes

Random sort within an already sorted query

If we use: $args = array( 'post_type' => 'custom_post_type', 'post_status' => 'publish', 'posts_per_page' => 9, 'meta_key' => 'pkg_id', 'orderby' ...
birgire's user avatar
  • 67.5k
2 votes
Accepted

How to tag the main tag?

You can't make a main tag per post. Tags are a not hyerarchycal taxonomy, and by definition, not hyerarchycal taxonomies have no hyerarchy, none of the terms can be more important than each other. In ...
David Navia's user avatar
2 votes
Accepted

sort query results by newest

WP_Query(array('post_type'=>'portfolio_item', 'post_status'=>'publish', 'posts_per_page'=>-5, 'order' => 'ASC')); See: https://codex.wordpress.org/Class_Reference/WP_Query#Order_....
Tex0gen's user avatar
  • 572
2 votes

Sort posts by tags in category pages

There really is no sane way to accomplish this, specially if you have posts that are assigned to more than one tag (which is almost always the case). What is very sure, you have you work cut out for ...
Pieter Goosen's user avatar
2 votes
Accepted

Sort a custom post with ACF: Date Picker & Display Featured!

Please change your args like this. $meta_query = array( array( 'key' => 'event_type', 'value' => 'featured', 'compare' => '=' ) ); $args = array( '...
Faysal Mahamud's user avatar

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