I have an archive page ordered by a numeric value in a custom field. This returns the ordered posts correctly, but does not display posts that don’t have a custom field.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("paged=$paged&cat=7&posts_per_page=24&meta_key=custom_order&orderby=meta_value_num&order=ASC");
What is the best way to return the ordered posts correctly, followed by any posts that do not have a custom field value associated with them?
Edit: Just to elaborate — The final result that I want to accomplish is a category archive page that has specific posts show up first, followed by the rest. Pretty much like sticky posts, but only for a specific category archive.
Edit Two: I am trying Jessica’s suggestion, and I think we’re almost there.
The problem is that now if I set to order=ASC
then all of the posts that have the custom field filled, show up after the posts that don’t have a value associated. If I set order=DESC
then the posts that have a custom field value are returned first, but in reverse. So, it would appear 4, 3, 2, 1, and then the rest of the posts with no value associated. How can I correct the order, so it shows 1, 2, 3, 4, and then the rest of the posts with no value in the custom_order
field?
I have added the following to my functions.php:
function wpse_55791_custom_order($clauses)
{
global $wp_query;
// check for order by custom_order
if ($wp_query->get('meta_key') == 'custom_order' && $wp_query->get('orderby') == 'meta_value_num')
{
// change the inner join to a left outer join,
// and change the where so it is applied to the join, not the results of the query
$clauses['join'] = str_replace('INNER JOIN', 'LEFT OUTER JOIN', $clauses['join']).$clauses['where'];
$clauses['where'] = '';
}
return $clauses;
}
add_filter('get_meta_sql', 'wpse_55791_custom_order', 10, 1);
function wpse_55791_custom_orderby($orderby)
{
global $wp_query;
// check for order by custom_order
if ($wp_query->get('meta_key') == 'custom_order' && $wp_query->get('orderby') == 'meta_value_num')
{
$orderby = "{$wpdb->postmeta}.meta_value='', ".$orderby;
}
return $orderby;
}
add_filter('posts_orderby', 'wpse_55791_custom_orderby', 10, 1);
When you specify a meta_key,
query_posts()
does an INNER JOIN between the wp_posts and wp_postmeta table. That means that any posts that don’t have any meta value for the key you specified won’t ever be returned in that query.In order to do what you need, you should use the same query you posted in your question, but change
orderby=meta_value
toorderby=meta_value_num
. Then you can filter ‘get_meta_sql’ to make the join return all posts. Add the following to your functions.php:EDIT: To fix the ordering, try adding this along with the above:
EDIT TWO – 2 loops:
Here’s how I’d do it:
Note that if you think there are ever going to be more than 24 ordered posts, the paged variable will be wrong for the unordered posts; you might need to set a global variable to keep track of how many ordered / unordered posts have been displayed so far and use that to calculate separate
$paged
values for each type.You could put the post IDs in an array and then query for all posts excluding those IDs:
I solved this in a very easy way, using an array for the ordering, as below:
That should fix it 🙂
This is a good workaround even if I dont like it at all. It’s again the limitation of the query api of wordpress. I loved to do it with mysql in the old ages.. but messing with WP is sometimes..you know
In terms of easiness and as my solution is for the admin-panel it doesn’t focus on efficiency. What I did is loading all posts and then sorting it using php.
Maybe its helpful to someone. 🙂
Even if the question is very old, it seems not to be answered yet (see Edit 2 in the above question).
I wasn’t happy with the previous solutions so I used nested meta queries to solve the problem.
In Words: Before querying posts, query those posts, that have custom_field set and those, that don’t have custom_field set (or set to null). (Query both, elsewise one of them will be ignored.) Then sort by custom_field ascending.