meta_query key value from array

I don’t know if you can get one array to compare to the other directly, but you can create a loop to set up a meta_query array that will check for each of the IDs within the field separately: $tag_ids = wp_get_post_tags( $post->ID, array( ‘fields’ => ‘ids’ ) ); $meta_query = array(‘relation’ => ‘OR’); foreach … Read more

Return posts with specific meta key first, but the rest in alpha order by last name

This may not be the best method, but I was able to achieve my desired result. I ended up setting up a second query. functions.php // List staff member by last name function posts_orderby_lastname ($orderby_statement) { $orderby_statement = “RIGHT(post_title, LOCATE(‘ ‘, REVERSE(post_title)) – 1) ASC”; return $orderby_statement; } staff-grid.php echo ‘<table>’; echo ‘<tr>’; // Query … Read more

Trying to perform complex custom field query with order by set to field value

If the value of your custom fields is numeric, you could try ordering with meta_value_num. Also note that both meta_value and meta_value_num require meta_key in the query as described here. $args = array( ‘post_type’ => ‘post’, ‘posts_per_page’ => $number, ‘meta_query’ => array( ‘relation’ => ‘OR’, array( ‘meta_key’ => ‘event-start’, ‘value’ => $today_is, ‘type’ => ‘NUMERIC’, … Read more

Search multiple meta keys at once

You’ll want to use the ‘meta_query’ argument of WP_Query ( http://codex.wordpress.org/Class_Reference/WP_Query ) Here’s a snippet that uses two separate meta key comparisons: $query_args = array( ‘post_type’ => ‘event’, ‘order’ => ‘ASC’, ‘orderby’ => ‘meta_value_num’, ‘meta_key’ => ‘_start_date’, ‘meta_query’ => array ( array( ‘key’ => ‘_start_date’, ‘value’ => $_start_of_month, ‘compare’ => ‘>’, ), array( ‘key’ => … Read more

WP_Query meta_query where meta value ends in space

Note sure how your structure is, but here’s one way, using RLIKE comparison and a space character class: array( ‘key’ => ‘_address_postcode’, ‘value’ => ‘^SC1[[:space:]]’, // Starts with ‘SC1 ‘ ‘compare’ => ‘RLIKE’ ) Maybe you should consider adjusting the meta values, as suggested by @cybmeta? But note that meta queries can be slow, so … Read more