Meta Query for specific months

You need the 2 digit month number that you want to query on and then use the code below. This should be easy with php (for example, see this post). In the code below $month is the number of the month in a 2 digit format, eg March would be 03. $start_date = date(‘Y’.$month.’01’); // … Read more

ACF Custom Field WP_Query, but need to get all posts, if field doesn’t exist

You can get both with an OR meta_query that also checks if the key does not exist: ‘meta_query’ => array( ‘relation’ => ‘OR’, array( ‘key’ => ‘event_date’, ‘compare’ => ‘NOT EXISTS’, ), array( ‘key’ => ‘event_date’, ‘value’ => date(“Ymd”, time()), ‘compare’ => ‘>’, ), ), ‘orderby’ => array( ‘meta_value_num’ => ‘DESC’, ‘date’ => ‘ASC’, ),

Sorting posts by custom fields in meta_query

As of WordPress 4.2, you can combine meta_query and orderby. The following example is taken from make.wordpress.org. Example of a simple meta_query: $q = new WP_Query( array( ‘meta_query’ => array( ‘relation’ => ‘AND’, ‘state_clause’ => array( ‘key’ => ‘state’, ‘value’ => ‘Wisconsin’, ), ‘city_clause’ => array( ‘key’ => ‘city’, ‘compare’ => ‘EXISTS’, ), ), ‘orderby’ … Read more

How to combine meta_query and post__in in WP_Query

post__in and post__not_in are mutually exclusive. Note: you cannot combine post__in and post__not_in in the same query. http://codex.wordpress.org/Class_Reference/WP_Query Either make the query with post__in and meta_query combination or make the query with post__not_in and meta_query combination. Any of them but not the union of them.

Programmatically set ‘meta_query’ for filter

A meta_query has to be an array of arrays. You only have an array. A filter needs to return information. Yours doesn’t. Of course, parse_query is an action not a filter, despite your use of add_filter. Use add_action instead. I would use pre_get_posts instead of parse_query. It is the next hook that fires after parse_query … Read more

Advanced Search by minimum/maximum values

A meta_query is an array of arrays. You only have an array. $query->set(‘meta_query’, array( ‘key’ => ‘shru_price’, ‘value’ => $_GET[‘minPrice’], ‘compare’ => ‘>=’, ‘type’ => ‘NUMERIC’ )); It should be: $query->set( ‘meta_query’, array( array( ‘key’ => ‘shru_price’, ‘value’ => $_GET[‘minPrice’], ‘compare’ => ‘>=’, ‘type’ => ‘NUMERIC’ ) ) ); And please validate/sanitize that user supplied … Read more

How can I apply a meta query for a single custom post type in the main query?

In the case that the start_date custom field only exists for the event post type, this works: $query->set( ‘meta_query’, array( ‘relation’ => ‘OR’, array( ‘key’ => ‘start_date’, ‘compare’ => ‘NOT EXISTS’, ), array( ‘key’ => ‘start_date’, ‘value’ => $today = date( ‘Ymd’ ), ‘compare’ => ‘>=’, ), array( ‘key’ => ‘end_date’, ‘value’ => $today = … Read more

Comparing between a negative and positive number

I tried the following code: $posts = get_posts([ “post_type” => “CUSTOM_POST_TYPE”, “meta_query” => [ ‘relation’ => ‘AND’, [ ‘key’ => ‘longitude’, ‘value’ => [-0.9895, 1.3125], ‘compare’ => ‘BETWEEN’, “type” => “NUMERIC”, ], ], ]); When I remove “type” => “NUMERIC”, I could reproduced your problem because the comparison is string based. But when adding the … Read more

Undefined property: WP_Query::$post

First of all post field of WP_Query is current post ID and not post object. But I don’t think you should use it before calling the_post() method. Normally you should do it in this way: $args = … $hometeams = new WP_Query( $args ); $teamishome = $hometeams->have_posts(); while ( $hometeams->have_posts() ) { $hometeams->the_post(); $scorehome = … Read more

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