Query posts with custom field is array - wordpress

I'm using ACF to create custom field of select type. I used like this to create custom select field like this:
1 : Anh
2 : Hai
3 : Hoang
4 : May
....
12 : Lan
I use this query to search posts have value is 1 (Anh) like this:
URL for query : /?thename=1
$thename = $_GET['thename'];
$args = array(
'post_type' => 'interviews',
'post_status' => 'publish',
'posts_per_page' => $perpage,
'paged' => get_query_var('paged'),
'meta_query' => array(
'key' => 'thename',
'value' => $thename,
'compare' => 'LIKE',
)
I searched like this but it returns wrong results. I think the value of thename field is array (value and label) then It is not possible to use query above.
So can you suggest me to do with this search? Thanks.

Your meta query should be an array within an array... update the meta_query like below:
'meta_query' => array(
array(
'key' => 'thename',
'value' => $thename,
'compare' => 'LIKE',
)
)

According to the meta_query documentation meta_query must contains one or more arrays, so your query must be something look like this :
'meta_query' => [
[
'key' => 'thename',
'value' => $thename,
'compare' => 'LIKE',
]
]

Related

Sort by custom meta_key in WordPress get_posts(), and include posts that do not have the meta_key

I have set on my WordPress post custom meta called "my_sort" which have a numeric values like 1, 2, 3, 4..... See below image:
I want to sort in ascending order by this meta "my_sort". This is the below code I am applying:
<?php
$catPost = get_posts(array('category' => get_cat_ID($categories[0]->name), 'meta_key' => 'my_sort', 'orderby' => 'meta_value_num', 'order' => 'ASC', 'numberposts' => 100)); //change this
?>
The problem with this code is that it is working but has one problem which I need to be fixed.
It is leaving all the other posts which do not have "my_sort" meta in them. I want to include those posts also. I want:
First the posts that have "my_sort" must come in ascending order.
Then the post that do not have "my_sort" meta should come.
Update
When I try the below query.
$catPost = get_posts(
array (
'category' => get_cat_ID($categories[0]->name),
'numberposts' => 100,
'orderby' => 'meta_value_num',
'meta_type' => 'NUMERIC',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key'=>'my_sort',
'compare' => 'EXISTS'
),
array(
'key'=>'my_sort',
'compare' => 'NOT EXISTS'
)
),
)
);
I do not get proper result. See the image which shows the result.
Here:
Record1 : does not have "my_sort" meta.
Record2 : does have "my_sort" and it's value is 2.
Record3 : does not have "my_sort" meta.
Record4 : does have "my_sort" meta and it's value is 4.
The result in this case should be like this:
Record2 then Record4 then Record1 then Record4 but clearly this is not the case.
What is wrong?
If you want to sort by a meta key that might not have any values, you need to use a meta_query that combines the results of a search for posts with the key and a search for posts without the key.
This isn't tested, but the main logic is there:
$catPost = get_posts(
array (
'category' => get_cat_ID($categories[0]->name),
'numberposts' => 100,
'orderby' => 'meta_value_num',
'meta_type' => 'NUMERIC',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key'=>'my_sort',
'compare' => 'EXISTS'
),
array(
'key'=>'my_sort',
'compare' => 'NOT EXISTS'
)
),
)
);

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 with Array using LIKE

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?

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.

Resources