WordPress Query with Array using LIKE - wordpress

I have a query that looks like this:
$data = get_posts( array(
'post_type' => 'custom_type',
'post_status' => 'any',
'posts_per_page' => 200,
'order' => 'ASC',
'meta_key' =>'_customer_start',
'orderby' => 'meta_value',
'meta_query' => array(
array(
'key' => '_customer_id',
'value' => $customer_id,
'compare' => 'IN'
),
array(
'key' => '_customer_start',
'value' => $_customer_start,
'compare' => 'LIKE'
)
)
)
);
The input to this query is two arrays:
$customer_id = array(2808,2814);
$_customer_start = array('20170212','20170224');
My issue is, this generates the following error:
Warning: trim() expects parameter 1 to be string, array given in ..../wp-includes/class-wp-meta-query.php on line 597
I have troubleshot this and it would appear the issue is the second array, passing in a group of dates for a LIKE comparison. I cannot use an IN comparison (As is recommend for arrays) as the dates stored include times. Hence if I use an IN or EQUALS, I will not find them.
Is there a way of passing an array for LIKE comparison, without dynamically expanding the query?

Related

Wordpress: compare one date coming as custom field with current date

I would like to know how to compare a date coming as custom field with the current date in a custom loop. I have following script, but it does not work...
enter$args = array(
'post_type' => 'books',
'meta_key'=>'course_date',//the format comes like this Ydm
'meta_value'=> date('d.m.Y'),
'meta_compare'=> '<'
);
My goal is to show all books with when the date is > then the current date. If the current date is > then the related post must be hidden.
Use this Query to get past event list ( 'compare' => '<') and get future events ( 'compare' => '>=')
$event_args = array(
'post_type' => 'books',
'meta_query' => array(
array(
'key' => 'course_date',
'value' => date('Ymd'),
'type' => 'DATE',
'compare' => '<'
)
),
'paged' =>$page,
'meta_key' => 'course_date',
'orderby' => 'meta_value',
'order' => 'ASC',
);

ACF query posts where post object is NULL / false

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.

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

wordpress query posts by meta date value

I am trying to query a custom post type by a custom field that contains a date. My query looks like this:
query_posts(
array(
'showposts' => 500,
'post_type' => 'holidays',
'post_status' => 'publish',
'order' => 'desc',
'orderby' => 'meta_value',
'meta_query' => array
(
array
(
'key' => 'from_date',
'value' => array( '01-01-2012', '31-12-2014' ),
'type' => 'DATE', // TRIED: DATE, SIGNED, NUMBER
'compare' => 'BETWEEN'
)
),
));
My custom fields store the date like this: 19-11-2013
When i run the query no results are shown, although dates within the range exist.
Am i approaching this is the correct manor, or am i missing something?
I can confirm Dan White's answer is correct, when querying by a date range the required format needs to be 'YYYY-mm-dd'. Your final code should look like this:
query_posts(
array(
'showposts' => 500,
'post_type' => 'holidays',
'post_status' => 'publish',
'order' => 'desc',
'orderby' => 'meta_value',
'meta_query' => array
(
array
(
'key' => 'from_date',
'value' => array( '2012-01-01', '2014-12-31' ),
'type' => 'DATE',
'compare' => 'BETWEEN'
)
),
));

Wordpress query_posts orderby meta queries

This should be simple, but I just can't pinned down a good example of the correct syntax to do this.
I want to filter my posts by the meta_queries but order them by the specified meta_key.
When I run the code as is it results an infinite loop. I only included the problem code the other code is your basic Loop code that the query_post runs in.
Also all the PHP variables are correct and are not the problem.
$args2 = array(
'meta_key' => '_count-views_all',
//'meta_value' => $id,
'orderby' => 'meta_value_num',
'order' => $sortOrder,
'posts_per_page' => 9,
'paged' => $paged,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'contributorid1',
'value' => $id,
'compare' => '='
),
array(
'key' => 'contributorid2',
'value' => $id,
'compare' => '='
)
)
);
$posts = query_posts($args2);
}
Here is another query that works completely without issue to cross reference. The two run on the same page but the are nested in an if else statement
$args1 = array(
//'meta_key' => 'contributorid1',
//'meta_value' => $id,
'order' => $sortOrder,
'orderby' => 'title',
'posts_per_page' => 9,
'paged' => $paged,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'contributorid1',
'value' => $id,
'compare' => '='
),
array(
'key' => 'contributorid2',
'value' => $id,
'compare' => '='
)
)
);
$posts = query_posts($args1);
The query looks reasonable to me. The only method by which I see this running into an infinite loop is if this query runs within the post loop. When you use query_posts as you are, it will change the state of the global $wp_query, which is used for the pointer in the main posts loop.
If it kept hitting query_posts within the loop, it would continually change the state of the global $wp_query object, and reset the pointer for the current post to the first post of that new query, which would ultimately create the infinite loop.
If this code is being used within the loop, I'd recommend instead using something like
$query = new WP_Query($args2);
if ($query->have_posts()) { ... etc; }
If you need to then set up global post data within it, be sure to use wp_setup_postdata or $query->the_post() and wp_reset_postdata or wp_reset_query appropriately when you are finished using that post as the global post information.

Resources