ACF query posts where post object is NULL / false - wordpress

The code below should hopefully make it clear what I'm trying to achieve. The key issue is with the second array in the meta_query. I am trying to find posts where the field 'alias' has not had a post_object set.
When running the query with var_dump( get_field('alias') ); the results returned are 'NULL'. I can't figure out how to query based on a NULL post object field. Pointers would be really appreciated.
$args = array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'game',
'value' => 'baseball',
'compare' => '='
),
array(
'key' => 'alias',
'value' => NULL,
'compare' => '='
)
)
);

You're comparing it wrong. You can't check for null in meta_query. Try this:
array(
'key' => 'alias',
'compare' => 'NOT EXISTS'
)
This is basically the SQL way of saying is null.

Related

ACF subfield in get_posts

I'm looking to query ACF subfields from a group within a group.
Here's my (stripped) code to show where I'm trying to target:
acf_add_local_field_group(array(
'key' => 'events_infos',
'fields' => array(
'key' => 'general_infos',
'type' => 'group',
'sub_fields' => array(
array(
'key' => 'dates',
'sub_fields' => array(
array(
'key' => 'start', // This is my sort key
...
)
)
)
)
));
I want to query the upcoming events chronologically so I would need to access; how would I format my key in the following meta_query?
get_posts(array(
'meta_query' => array(
array(
'key' => **events_infos[general_infos][dates][start]**,
'order' => 'ASC'
)
)
)
As indicated by #Stender in his comment, the answer can be found in the documentation under the heading 4. Sub custom field values".

WP_Query multiple values with Meta key?

I am really facing so much problem in fetching result of multiple values and single meta key.
Example:
I have 10 Post of 'EGG' word.
I have 5 Post of 'Butter' word.
Now i am using below query to fetch result. It suppose to show 15 post in result but its not showing total result.
$args = array(
'numberposts' => -1,
'post_type' => 'recipes',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ingredients_repeat_%_name',
'compare' => 'LIKE',
'value' => 'Butter',
),
array(
'key' => 'ingredients_repeat_%_name',
'compare' => 'LIKE',
'value' => 'Egg',
),
)
);
Instead of using the numberposts try posts_per_page instead. You can learn more about WP_Query

eventon select upcomming event with WP_Query

How do I query only upcoming events and to look in them? eventOn does not give a function for that.
My code is:
$events = new WP_Query(array(
'post_type' => 'ajde_events',
'meta_query' => array(
array(
'key' => 'evcal_srow',
'value' => time(),
'compare' => '>',
'type' => 'numeric',
)
),
));
You're using start date to compare. It's incorrect way to get up coming events.
Change 'key' => 'evcal_srow' to 'key' => 'evcal_erow' will give correct results of upcoming events.

WP_Query meta_query argument

I am trying to write a 'complex' WP_Query but it keeps failing. The meta_query argument Im using is:
$query_args['meta_query'] = array(
"relation" => "AND",
array("relation" => "OR",
array(
'key' => '_htp_hide_trending_posts',
'value' => 'on',
'type' => 'CHAR',
'compare' => '!='
),
array(
'key' => '_htp_hide_trending_posts',
'compare' => 'NOT EXISTS'
)
),
array(
'key' => 'views',
'value' => 0,
'type' => 'NUMERIC',
'compare' => '>'
)
);
_htp_hide_trending_posts is a custom meta_value. If I ignore the views part and just use the OR statement, it's ok but adding the ADD relation and it then ignores the OR relation search. I need both as it needs to order by views and that doesn't work if it's not in the meta_query. I'm going round in circles, hoping someone can spot an obvious mistake?
I finally worked out a solution:
$query_args['meta_query'] = array(
array(
'key' => '_htp_hide_trending_posts',
'compare' => 'NOT EXISTS'
)
);
$query_args['meta_key'] = 'views';
$query_args['meta_compare'] = '>';
$query_args['meta_value'] = '0';
$query_args['orderby'] = array('meta_value_num' => 'DESC');
I didn't need the OR query which seemed to do the trick.

Wordpress Meta Query to pull posts with empty meta value for custom fields

I am trying to pull custom post type with field value set as 'No' or not set (older posts for which this custom field is not set). The code i am using for query args is:
$args = array_merge( $wp_query->query_vars, array( 'post_type' => 'com', 'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ct_Featured_C_radio_3292',
'value' => 'No',
'compare' => 'LIKE'
),
array(
'key' => 'ct_Featured_C_radio_3292',
'value' => array(''),
'compare' => 'LIKE'
)
)));
But this meta query only pulls posts with value set as 'No' and not with posts with empty values. Please advice on how to write the meta query so all posts with No and empty values can be pulled.
"NOT EXISTS" checks will include meta_keys that don't exist.
$args = array_merge( $wp_query->query_vars, array( 'post_type' => 'com', 'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ct_Featured_C_radio_3292',
'value' => 'No',
'compare' => 'LIKE'
),
array(
'key' => 'ct_Featured_C_radio_3292',
'compare' => 'NOT EXISTS'
)
)));
Try This
http://codex.wordpress.org/Class_Reference/WP_Meta_Query
This is related to meta Query

Resources