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 ...
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 ){
...
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 ...
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. ...
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....
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 ...
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(
...
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 ...
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 "...
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 ...
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' ) ...
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 ...
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 ...
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-&...
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 ...
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 ...
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 ...
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->...
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 ...
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.
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($...
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 ...
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 $...
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', '...
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 ...
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', // ...
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')
{
...
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 ...
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. ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
sort × 529wp-query × 107
custom-post-types × 95
order × 83
custom-field × 74
posts × 48
categories × 37
php × 36
query × 34
loop × 30
query-posts × 29
custom-taxonomy × 28
meta-query × 26
filters × 24
post-meta × 24
columns × 24
woocommerce-offtopic × 23
admin × 21
wp-admin × 19
taxonomy × 18
advanced-custom-fields × 18
pre-get-posts × 18
customization × 15
archives × 15
search × 14