I want to build a dynamic set of arguments for a wp_query but have had real problems doing this. Take the following example code (which does work)...
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'skills',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'years',
'value' => 'Primary',
'compare' => 'LIKE'
),
array(
'key' => 'years',
'value' => 'Secondary',
'compare' => 'LIKE'
)
)
);
// get results
$the_query = new WP_Query( $args );
The idea is that depending on the results of user input in a form, the number of clauses in my meta_query value would alter. In the code above there are two options, but maybe depending on input there are 3 or another time 5.
I tried compiling these inner array elements externally of $args. Assume in following code that $inputArray is a single dimensional array of string elements. The test looked like:
$inputArray=array();
if (is_array($yearsArray)){
foreach( $yearsArray as $year ) {
$inputArray[]=array('key' => 'years','value' => $year,'compare' => 'LIKE');
}
}
// args
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'skills',
'meta_query' => array(
'relation' => 'OR',
$inputArray
)
);
But the query just runs as if no meta_query queries had been applied.
I also tried using the function wp_parse_args to try and merge multiple meta_queries, however it seems that I'm overwriting the value and only the last one is ever used. This looked like this:
// args
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'skills'
);
$args2 = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'years',
'value' => 'Secondary',
'compare' => 'LIKE'
)
)
);
$args3 = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'years',
'value' => 'Primary',
'compare' => 'LIKE'
)
)
);
$args=wp_parse_args($args,$args2,$args3);
So as you can see I've tried a few different methods but none are working. Can anyone assist?
I fixed this by discovering that you can use IN as a compare clause. $yearsArray was just a single dimensional array of string values.
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'skills',
'meta_query' => array(
array(
'key' => 'years',
'value' => $yearsArray,
'compare' => 'IN'
)
)
);
Related
Why does this return all posts;
$partner = get_query_var('partner');
echo "Partner ID: ". $partner; // Echoes correctly
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
'meta_key' => 'partner',
'meta_value' => array($partner), // also $partner (without array)
'compare' => 'LIKE', // Also 'IN'
),
);
$partner_query = new WP_Query($args);
the ACF field 'partner' is an array, the query variable is a string (obv)
The correct usage of meta_query is like this:
The args shouldn't be meta_key if you're using meta_query - they should be key, value, etc.
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'partner',
'value' => array( $partner ),
'compare' => 'IN',
),
),
);
Try like this:
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
'meta_key' => 'partner',
'meta_value' => array($partner), // also $partner (without array)
'compare' => '=', // Also 'IN'
),
);
$partner_query = new WP_Query($args);
For anyone who gets stuck in this rabbit hole.
My query parameter is a string, ACF stores the IDs as a string if there's one and an array if there's more than one.
This form of the arguments returns the correct results. (many thanks to Howard E and IronCanTaco)
$partner = get_query_var('partner'); // string
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'partner', // Could be string or array
'value' => $partner,
'compare' => 'LIKE',
),
),
);
I need help with a WP_Query. I use the meta query argument using the OR and AND relations, but this argument seems to be ignored in the result of the query.
Here is my code :
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'my_custome_post_type',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array( 'author' => $contact_id ),
array( 'meta_key' => 'my_meta', 'meta_value' => $user_id )
),
array(
'relation' => 'AND',
array( 'author' => $user_id ),
array( 'meta_key' => 'my_meta', 'meta_value' => $contact_id )
)
)
);
$query = new \WP_Query( $args );
$response = $query->posts;
I already tried to add this argument like suggested in here:
'suppress_filters' => false,
'is_your_custom_query' => true, // your custom flag
Even if I remplace the value of $user_id and $contact_id directly in the query with any number, the query still return the same result. I don't understand why it's not working.
Thank you for your help !
As suggested by dafoxuk, I have to remplace meta_key by key and meta_value by value. I also had to add 'compare' => 'LIKE'.
But event in this case it wasn't working. I also had to stock the author_id in a post_meta, and change the array( 'author'=> $contact_id ) condition by :
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
So the final $args array look like this :
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'my_custome_post_type',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
array( 'key ' => 'my_meta', 'value' => $user_id, 'compare' => 'LIKE '
array(
'relation' => 'AND',
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
array( 'key ' => 'my_meta', 'value' => $contact_id, 'compare' => 'LIKE' )
)
);
I want a query that returns CPT posts that contains the user's input (which is $text).
Inserting meta_key and meta_value works well but putting it in a meta_query array doesn't return anything. I want to be able to search in multiple custom fields. It's a theme I made from scratch so there is no plugins and the functions.php file is pretty small so I don't think it could be a conflict.
Code for meta_key and meta_value in the query declaration (working):
$searchquery = new WP_Query(
array( 'post_type' => 'offre',
'meta_key' => 'offre_employeur',
'meta_value' => $text,
'meta_compare'=> 'LIKE' )
);
Code for meta_value array (not working):
$meta_query_args = array(
'relation' => 'OR',
array(
'key' => 'offre_employeur',
'value' => $text,
'compare' => 'LIKE'
), array(
'key' => 'offre_titre',
'value' => $text,
'compare' => 'LIKE'
)
);
$searchquery = new WP_Query(array('post_type' => 'offre'));
$searchquery->set('meta_query', $meta_query_args);
Code for the second way I tried but still no results (not working)
$args = array(
'post_type' => 'offre',
'posts_per_page' => -1,
's' => $text,
'meta_query' => array(
array(
'key' => 'offre_employeur',
'value' => $text,
'compare' => 'LIKE'
),
array(
'key' => 'offre_titre',
'value' => $text,
'compare' => 'LIKE'
)
)
);
$searchquery = new WP_Query($args);
Thank you in advance for your time.
Per the docs here: https://codex.wordpress.org/Class_Reference/WP_Query
I would assert you want to scroll down to the section titled "Multiple Custom Field Handling:", which has this example, which most closely matches your situation:
$args = array(
'post_type' => 'product',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE',
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
);
$query = new WP_Query( $args );
Which, taken what you've provided in your question, I would modify as follows to get the results you are after:
$args = array(
'post_type' => 'offre',
'posts_per_page' => -1,
// remove the "search" query, which restricts the results to those with titles / content that match the $text content
// 's' => $text,
'meta_query' => array(
// add the "relation" argument, default is AND, you need OR
'relation' => 'OR',
array(
'key' => 'offre_employeur',
'value' => $text,
'compare' => 'LIKE'
),
array(
'key' => 'offre_titre',
'value' => $text,
'compare' => 'LIKE'
)
)
);
$searchquery = new WP_Query($args);
I'm breaking my head over here. Through a post on this website I've managed to create a custom taxonomy archive Page in WordPress. Now I'm trying to add dynamic checkbox filters to it, but I can't seem to get the meta_query working.
This line of code works like I would like it to work;
$query = array(
'post_type' => 'company',
'posts_per_page' => 999,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'company_category',
'field' => 'slug',
'terms' => $al_cat_slug
)
),
'meta_query' => array (
array (
'key' => 'company_method',
'value' => 'Online',
'compare' => 'LIKE',
)
)
);
How ever, this one won't:
$query = array(
'post_type' => 'company',
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'company_category',
'field' => 'slug',
'terms' => $al_cat_slug
)
),
);
$al_tax_post_qry = new WP_Query($query);
$meta_query = $al_tax_post_qry->get('meta_query');
$name = 'company_method';
$value = explode(',', $_GET[ $name ]);
$meta_query[] = array(
'key' => $name,
'value' => $value,
'compare' => 'LIKE',
);
$al_tax_post_qry->set('meta_query', $meta_query);
Whatever I enter in the URL, it keeps finding all the results and it won't filter like the first. A print_r($meta_query); gives me:
Array ( [0] => Array ( [key] => company_method [value] => Array ( [0] => Online ) [compare] => LIKE ) )
Edit 07-06-2016 // 09:00
After reading the comment stating that I should use 'IN', I've experimented a little further and when I use it withing the query itself, it gives me no results at all. It seems 'IN' is the issue.
The field I'm querying is an 'Advanced Custom Fields' field so that might have something to do with it? However the examples on their website also use the same method.
Not Working:
$query = array(
'post_type' => 'company',
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'company_category',
'field' => 'slug',
'terms' => $al_cat_slug
)
),
'meta_query' => array (
'relation' => 'AND',
array (
'key' => 'company_method',
'value' => array('online', 'orange', 'apple'),
'compare' => 'IN',
)
),
);
Working:
$query = array(
'post_type' => 'company',
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'company_category',
'field' => 'slug',
'terms' => $al_cat_slug
)
),
'meta_query' => array (
'relation' => 'AND',
array (
'key' => 'company_method',
'value' => 'online',
'compare' => 'LIKE',
)
),
);
Optionally I could create an array entry in the meta_query for every value in the array, but that might not be ideal.
If you define your $meta_query value as an array, you should change your compare operator. IN and NOT IN are the array specific ones.
Try this:
$meta_query[] = array(
'key' => $name,
'value' => $value,
'compare' => 'IN',
);
Hope it helps!
With help from this post, the issue is solved! Values are stored in a serialized way, so need a little different approach.
https://wordpress.stackexchange.com/questions/183182/meta-query-compare-in-not-working
I have a ACF select field which takes multiple value. Now I want to use get_posts() to get those custom posts. My arguments look like this:
$party = 'test1';
$function = 'test1, test2';
$args = array(
'numberposts' => -1,
'post_type' => 'event',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'party',
'compare' => '=',
'value' => $party,
),
array(
'key' => 'function',
'compare' => 'IN',
'value' => array($function),
)
)
);
$items = get_posts($args);
But this does not work! Don't know what is wrong here!