querying with custom meta field with meta_query

There’s actually a better solution that will (hopefully) be rolling out in WordPress 3.4 — you can run the patch as a hotfix now if you’d like, but here’s the TRAC link for the patch: http://core.trac.wordpress.org/ticket/18158 With this, you can do … $my_query = new WP_Query( array( ‘meta_query’ => array( array( ‘key’ => ‘foo’, ‘compare’ … Read more

Perform query with meta_value date

I did something similar, the technique you need to use is called a meta query. Here is the query I wrote to get posts based on a date value stored as a custom field meta value. query_posts( array( ‘post_type’=>’post’, ‘order’=>’ASC’, ‘orderby’=>’meta_value_num’, ‘meta_key’=>’date_event’, ‘posts_per_page’=> -1, ‘meta_query’ => array( ‘relation’ => ‘AND’, array( ‘key’ => ‘date_event’, ‘value’ … 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

Order Posts by Taxonomy and Meta Value

In your code, there is if ( isset( $wp_query->query[‘orderby’] ) && $taxonomy == $wp_query->query[‘orderby’] that make your code not work with multiple order fields, because if you set ‘orderby’ => ‘taxonomy_cat meta_value’ then $taxonomy == $wp_query->query[‘orderby’] is never true. In addition, I find that looping all taxonomy only with the aim to find the taxonomy … 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

WP_Query using meta_query with relation OR and orderby meta_value doesn’t work

The issue is that WordPress are getting all posts that has a ‘startDate’ meta key, no matter the meta value. You can understand that form this part of the request: … AND ( wp_postmeta.meta_key = ‘startDate’ OR ( mt1.meta_key = ‘startDate’ AND CAST(mt1.meta_value AS CHAR) >= ‘20140305’ ) OR ( mt2.meta_key = ‘endDate’ AND CAST(mt2.meta_value … Read more

ACF datepicker meta_query Compare Dates in m/d/Y g:i a – Not in Ymd Format

There should not be any need to do this. Even if an ACF Field is using ‘return_format’ => ‘m/d/Y g:i a’, The post_meta value is in YYYY-MM-DD 00:00:00 format. $date_now = date(‘Y-m-d’); $args = [ ‘meta_key’=>’the_date’, ‘meta_value’=>$date_now.’ 00:00:00′, ‘meta_compare’=>’>=’, ]; $query = new WP_Query( $args ); Edit: I’ve noticed some discrepancies in this, the value … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)