Sort by custom field, or lack thereof
-
Hi, I’ve found lots of posts about sorting by the value of a custom field, most of which miss the fact that the wp_query class now makes this a bit easier using meta_value_num – but I haven’t been able to find anything to sort by a meta_value without excluding all posts lacking the relevant meta_key. What I want is a page which lists posts with a ‘priority’ value set first, but includes those without. All the ways I’ve seen of sorting my the value of a custom field rely on directly or indirectly calling SQL with the equivalent of ‘WHERE meta_key=priority’ so that it knows which key’s value to use.
What I have at the moment is this (adapted from the PageOfPosts template found as an example here on WordPress.org):
$args=array( 'category__in' => array($cat), 'meta_key' => 'priority', 'orderby' => 'meta_value_num, date', 'order' => 'DESC', 'paged' => $paged, 'posts_per_page' => $post_per_page, 'caller_get_posts' => $do_not_show_stickies ); $temp = $wp_query; // assign orginal query to temp variable for later use $wp_query = null; $wp_query = new WP_Query($args);
I guess I could just call this twice, once with the above and once excluding posts with the relevant custom field, but that seems awfully kludgey. Is there a neater way?
-
Maybe this will help: http://wordpress.org/support/topic/get_post-query-where-posts-dont-have-a-specific-meta_key-1?replies=8
in combination with meta_query available since Version 3.1
http://codex.wordpress.org/Function_Reference/WP_Query#Custom_Field_ParametersThanks… the first link gives some specifics on how I could exclude posts with this custom field from my query, which does take me to a working answer, but a kludgey one, as I said. And meta_query is nice – surprised I’d missed that – but I’m not seeing a way to use it to solve this problem with reasonably elegant code. Actually, I’m not seeing a way to use it here at all, which makes me wonder if I’m just being dense.
- The topic ‘Sort by custom field, or lack thereof’ is closed to new replies.