meta_query not working on live site

My reputation is too low for me to comment so I’ll do it this way. Make sure that the date format inside ‘value’ is the same as the date formats inside the database fields. Also don’t forget to add the ‘type’ => ‘DATE’ to your query. An example of some code that I’ve used in … Read more

Query posts with numeric meta values within a given range

I ended up finding two solutions within WordPress’ capabilities. Storing available rather than unavailable dates The first solution would be to have the agency select all available dates rather than selecting unavailable dates. Our example listing would then have a custom field listing_available: update_post_meta($post_id, “listing_available”, “/20140101/20140102/20140103/20140104/”); I would then be able to run the following … Read more

meta_query compare=’!=’ with multiple custom fields

This might require writing custom sql with a subquery: $results = $wpdb->get_results(” SELECT * FROM $wpdb->posts WHERE ID NOT IN ( SELECT post_id FROM $wpdb->postmeta WHERE meta_key = ‘my_custom_field’ and meta_value = 2 ) “); then just iterate through the results as described here.

Meta query with a sub query group possible?

I don’t think you need the relation parameter. It defaults to AND. $nowtime = time(); $the_query = new WP_Query( array( ‘post_type’ => ‘promos’, ‘orderby’ => ‘menu_order’, ‘order’ => ‘ASC’, ‘meta_query’ => array( array( ‘key’ => ‘wpcf-type-of-promo’, ‘value’ => ‘walls’, ‘compare’ => ‘NOT LIKE’ ), array( ‘key’ => ‘wpcf-date-and-time-start’, ‘value’ => $nowtime, ‘compare’ => ‘<‘, ‘type’ … Read more

Comparing a field with several values at once with meta_query

The parameter ‘meta_query’ is an array, so you can add multiple meta keys. In the doc, below the section “Example: Multiple Meta Entries – Multi dimensional array”, you can find an example where the relation is set to ‘OR’: $query_args = array( ‘meta_query’ => array( ‘relation’ => ‘OR’, array( ‘key’ => ‘foo_key’, // ‘value’ => … Read more

Custom post type archive with rand and meta_value

You could use the ‘posts_orderby’ filter: function custom_loops($query) { if ( is_post_type_archive(‘my_post_type’) ){ $query->set( ‘posts_per_page’, 40); $query->set( ‘meta_key’, ‘custom_meta_sponsored’); $query->set( ‘orderby’, ‘custom_meta_sponsored’); $query->set( ‘order’, ‘DESC’); add_filter( ‘posts_orderby’, function ( $orderby, $query ) { if ( $query->get( ‘orderby’ ) != ‘custom_meta_sponsored’ ) return $orderby; global $wpdb; $orderby = $wpdb->prepare( ‘CASE WHEN ‘ . $wpdb->postmeta . ‘.meta_value … Read more

Query Multiple values for same key

Shouldn’t it be like this (doc)? $meta_query[] = array( ‘meta_query’ => array( ‘relation’ => ‘AND’, array( ‘key’ => ‘estate_property_google_maps’, ‘value’ => $al[‘0’], ‘compare’ => ‘LIKE’ ), array( ‘key’ => ‘estate_property_google_maps’, ‘value’ => $al[‘1’], ‘compare’ => ‘LIKE’ ), ) );

WP_Query ordered by meta_value_num doesn’t fetch posts without this meta field

The reason you’re only getting those users that have that meta_key is because you’re include ‘meta_key’ => ‘…’ in $args. That limits results to those that have that key, regardless of any clauses you may have in a meta_query. This applies to WP_Query, WP_User_Query and WP_Comment_Query. Starting in WP 4.2, you can “name” the clauses … Read more