Dynamic meta_query - wordpress

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

Related

Get all terms from posts that have a certain post meta value

I am trying to get all terms from a custom post type (artist) that have a meta_key(performance_date_1) and a meta_value(20220430) that I created with ACF.
This is what I have tried so far.
$events = get_terms( 'artist_event', array(
'post_type' => 'artist',
'taxonomy' => 'artist_event',
'hide_empty' => false,
'suppress_filters' => false,
'meta_query' => array(
// 'relation' => 'OR',
array(
'key' => 'performance_date_1',
'compare' => '=',
'value' => '20220430',
)
),
) );
When using get_terms is the meta_query looking in taxonomy fields or post fields? I need the meta query to look for post fields to fix this issue I believe.
I had a similar situation. In my case, I resorted to using array_filter against posts via WP_Query::have_posts although this is certainly not the most performant solution.
$events = array_filter(
get_terms(array('taxonomy' => 'artist_event')),
function($term) {
return (new WP_Query(array(
'post_type' => 'artist',
'posts_per_page' => -1,
'tax_query' => array(
'taxonomy' => 'artist_event',
'field' => 'slug',
'terms' => $term->slug,
),
'meta_query' => array(
array(
'key' => 'performance_date_1',
'compare' => '=',
'value' => '20220430',
)
),
)))->have_posts();
}
);

Wordpress - WP Query : meta_query relation ignored

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' )
)
);

Wordpress meta_query not returning results

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);

How to make query with three different parameters

I have three different parameters to search with, first one is post title of custom post type, second is taxonomy term and third is post type also, I am able to search if three of them are selected, but how to search related records if two of them are selected only or just one of them ?
$args = array(
'post_type' => 'course',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'course-category',
'terms' => $cs_course_cate,
'field' => 'term_id',
)
),
'meta_query' => array(
array(
'key' => 'cs_course_campus_id',
'value' => $cs_campus,
'compare' => 'LIKE',
),
),
);
View Search Box Here
Try this code.
$args = array(
'post_type' => 'course',
'post_status' => 'publish'
);
if(!empty($cs_course_cate)){
$args = array('tax_query' => array(
array(
'taxonomy' => 'course-category',
'terms' => $cs_course_cate,
'field' => 'term_id',
)
));
}
if(!empty($cs_campus)){
$args = array('meta_query' => array(
array(
'key' => 'cs_course_campus_id',
'value' => $cs_campus,
'compare' => 'LIKE',
),
)
);
}

wp_query with a variable quantity of arguments

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'
)
)
);

Resources