LIKE %…% Meta Query

I came across this same issue today. In both our situations, there is in fact a nice solution. Because the serialized data we are searching for, are simply IDs, and they are always wrapped in quotations, we just need to include the quotations as part of the query. This eliminates the problem of encountering false … Read more

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

meta_query with multiple values

Expanding a bit on the answer from @dgarceran Generally, using the WP_Query class is probably a good idea for querying posts. We’ll want to pass arguments to that query. In your case we’ll likely want use one of the following: “Custom Field Parameters” – meta_query “Taxonomy Parameters” – tax_query For a nearly comprehensive example of … Read more

Best practice – Meta Query vs. post_clauses for “left join” ordering

You can use pre_get_posts with a callback: <?php defined( ‘ABSPATH’ ) OR exit; /** Plugin Name: (#102854) Order Posts by Foo */ add_filter( ‘pre_get_posts’, ‘wpse_102854_orderby_foo’ ); function wpse_102854_orderby_foo( $query ) { if ( ! $query->is_main_query() OR ! is_admin() OR ‘edit.php?post_type=YOUR_POST_TYPE’ !== $GLOBALS[‘parent_file’] // Other conditions that force an abort, Example: // OR ‘foo’ !== $query->get( … Read more

Meta query with multiple logic (AND / OR)

The correct syntax for performing the AND OR is as follows: ‘meta_query’ => array( ‘relation’ => ‘AND’, array( ‘key’ => ‘flagged’, ‘value’ => ‘1’, ‘compare’ => ‘!=’ ), array( ‘relation’ => ‘OR’, array( ‘key’ => ‘expiry_date’, ‘value’ => date(‘Y-m-d’,strtotime(“today”)), ‘compare’ => ‘>=’, ‘type’ => ‘DATETIME’, ), array( //has no date ‘key’ => ‘expiry_date’, ‘value’ => … Read more

get_users meta_query

meta_query parameter is an array of arrays, $args = array( ‘meta_query’=> array( array( ‘relation’ => ‘AND’, array( ‘key’ => ‘minbeds’, ‘value’ => $rooms, ‘compare’ => “<=”, ‘type’ => ‘numeric’ ), array( ‘key’ => ‘maxbeds’, ‘value’ => $rooms, ‘compare’ => “>=”, ‘type’ => ‘numeric’ ), array( ‘key’ => ‘minprice’, ‘value’ => $price, ‘compare’ => “<=”, ‘type’ … Read more