Hot answers tagged

17 votes

Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title

Few years later, the code posted by CSSGirl wasn't working for me because there were some posts that didn't have the meta key or the meta key was empty so this is what I had to do to have all the ...
user avatar
17 votes
Accepted

group search results by post type?

You can run the same loop multiple times by using rewind_posts() to output each type separately. if( have_posts() ){ $types = array('post', 'lesson', 'series'); foreach( $types as $type ){ ...
user avatar
  • 76.2k
13 votes
Accepted

ORDER BY custom field value

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 ...
user avatar
12 votes
Accepted

Orderby meta_value only returns posts that have existing meta_key

As stated in @ambroseya's answer, its supposed to work like that. Once you declare a meta query, even if you aren't looking for a specific value, it will only query posts with that meta key declared. ...
user avatar
12 votes

Ignoring initial articles (like 'a', 'an' or 'the') when sorting queries?

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....
user avatar
  • 4,865
11 votes
Accepted

How do I set the default admin sort order for a custom post type to a custom column?

Solution from a cross post over at StackExchange from @birgire: The problem is that you run the clientarea_default_order callback too late. To fix that you only have to change the priority ...
user avatar
8 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( ...
user avatar
8 votes
Accepted

Ignoring initial articles (like 'a', 'an' or 'the') when sorting queries?

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 ...
user avatar
  • 64.1k
7 votes
Accepted

WP Query orderby meta key natural sort?

No, not as a straight WP_Query. This is because of several factors, with the main one being that mySQL doesn't natively have a natural sort for alphanumeric columns. WordPress does support a "...
user avatar
  • 32.1k
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 ...
user avatar
6 votes

Sort posts by category name and title

To get them broken down by Category, you need to loop through the list of categories and then query on each category: $categories = get_categories( array ('orderby' => 'name', 'order' => 'asc' ) ...
user avatar
  • 391
6 votes

Drag n Drop Post Order for multiple Custom Post Types

It's common to utilize the WordPress navigational menu UI to get the drag/drop feature for free. I think there are already some answers, on this site, explaining this better than I can, but I can't ...
user avatar
  • 64.1k
6 votes
Accepted

Sorting posts according to view counts not working

Your code doesn't make sense. You use query_posts(), which you should never do, but all that does is clobber the main query. You don't use the clobbered query though. You then use get_posts() with ...
user avatar
  • 63.6k
6 votes
Accepted

Sort by meta key on archive page

Use pre_get_posts to alter the main query before it is run. The action runs on every query, use the Conditional Tags to target specific queries. function wpd_sort_by_meta( $query ) { if ( $query-&...
user avatar
  • 76.2k
6 votes

Ignoring initial articles (like 'a', 'an' or 'the') when sorting queries?

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 ...
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 ...
user avatar
6 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 ...
user avatar
5 votes
Accepted

custom sort posts in archive/taxanomy page

Put something along these lines in your functions.php: add_filter( 'pre_get_posts', 'custom_get_posts' ); function custom_get_posts( $query ) { if( is_category() || is_archive() ) { $query->...
user avatar
  • 391
5 votes
Accepted

Column sorting with emtpy meta values

The problem is that by setting the posts to be ordered by _start_event_datetime you're creating the implicit assumption that the post will have that meta. This means the SQL query WordPress generates ...
user avatar
5 votes

Sort plugins by rating

Well it doesn't. It was changed after year 2011 and now (in 2015) you have no options to sort on Wordpress.org or in your WordPress installation.
user avatar
  • 51
5 votes

WP Query orderby meta key natural sort?

In case someone happens to stumble upon this I'll give a you solution, that uses "faked natural sorting" (thanks to Otto) but it also has sorting by multiple custom fields. function orderbyreplace($...
user avatar
5 votes
Accepted

Multi-level ordering in Wordpress

Multiple orderby with different sort order values are possible as of WordPress 4.0, take a look at the codex page for WP_Query - Order - Orderby Parameters. There is an related Make WordPress Core ...
user avatar
5 votes
Accepted

How to get order of posts?

Found the answer from the WordPress docs. For what the query is ordered by, I use: global $wp_query; echo $wp_query->get('orderby'); For the order by which the posts are queried: global $...
user avatar
  • 397
5 votes
Accepted

How can I sort get_users() by any value (last_name, user defined fields and more)

get_users() does not allow you to directly sort by whatever you like, but there are ways around it. The Codex for get_users() says: orderby - Sort by 'ID', 'login', 'nicename', 'email', 'url', '...
user avatar
  • 1,034
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 ...
user avatar
  • 3,742
5 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', // ...
user avatar
  • 32.1k
4 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') { ...
user avatar
  • 215
4 votes

Sort users in get_users() in custom order

There is a simpler, faster method than the one by @Warface. This is an extention to @SyHolloway The concept here is to wrap get_users() in a new function in which you can sort the result from ...
user avatar
4 votes
Accepted

Sorting Posts by custom field

To expand on @pieter-goosen's comment, you do indeed want to use pre_get_posts. In your example, by using WP_Query, you're overwriting the entire query and just resetting most parts of it to default. ...
user avatar
  • 9,947

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