WP_Query with meta_query and orderby meta_value
-
Hi, i’m extracting a list of posts using the following “args” for a WP_Query:
$args = array( 'post_type' => array( 'post_type_1', 'post_type_2', 'post_type_3', 'post_type_4', 'post_type_5' ), 'posts_per_page' => -1, 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'position', 'value' => 'first', 'compare' => 'LIKE', ), array( 'key' => 'focus', 'value' => 'yes', 'compare' => 'LIKE', ) ) ); $query = new WP_Query($args);
On these five custom post types, i have one common custom field that’s “article_position”, for some post types i have a custom field named “position” and for the others i have a custom field named “focus”.
I use “position” and “focus” to retrive the list of posts from the group of five custom post types: these that have the value “first” for position or “yes” for focus will be on the queried list.
Till this point it works.
Now i have to order this list using another custom field named “article_position” that appears on all the custom post types, it’s common to all.So i’ve added a couple of code lines as here:
$args = array( 'post_type' => array( 'post_type_1', 'post_type_2', 'post_type_3', 'post_type_4', 'post_type_5' ), 'posts_per_page' => -1, 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'position', 'value' => 'first', 'compare' => 'LIKE', ), array( 'key' => 'focus', 'value' => 'yes', 'compare' => 'LIKE', ) ), 'meta_key' => 'article_position', 'orderby' => 'meta_value_num', 'order' => 'DESC' ); $query = new WP_Query($args);
But seems that the last lines don’t add anything so useful to reach my goal.
I’ve searched on codex and google but without success.
Any hint would be greatly appreciated, thank you!
- The topic ‘WP_Query with meta_query and orderby meta_value’ is closed to new replies.