I am trying to fetch the result if keyword, category and meta value is not empty. I mean to say that if the keyword is test and city is Mumbai and category is pet then show existing results that come in these parameters. Now I am getting all the results which have in other categories too.I have two inputs , one for keyword, second for City, zip code and third one for categories drop down.
Any suggestions would be greatly appreciated.
Expected result should be keyword,city under selected category.
$arg = array(
'post_type' => 'post',
'posts_per_page' => 10,
's' => $keyword,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' =>$cats
)
),
'meta_query' => array(
array( 'key' => 'city', 'value' => $query_city, 'compare' => 'LIKE' ),
array( 'key' => 'country', 'value' => $query_city, 'compare' => 'LIKE' ),
array( 'key' => 'postalcode', 'value' => $query_city, 'compare' => 'LIKE' ),
'relation' => 'OR'),
);
$query = new WP_Query( $arg );
If I understand you right, using 'relation' => 'AND' instead 'relation' => 'OR' should solve your issue.
Related
i want to make a filter, where i can list employees
however the employees are from post type employees (example) and their contracts are made in child post type called contracts ( example ) so i have made this code but i can't take all info that i need
$args = array( 'post_type' => array( 'employees', 'contracts'),
'posts_per_page' => -1,
'paged' => $paged,
'orderby'=>'title',
'order'=> $order,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'wpcf-podruznici',
'value' => $podruznici,
'type' => 'CHAR',
'compare' => '='
),
array(
'key' => 'wpcf-direkcija_podruznica',
'value' => $podruznici,
'type' => 'CHAR',
'compare' => '='
),
array(
'key' => 'wpcf-opredeneno_neopredeleno_vraboten',
'value' => $dogovor,
'type' => 'CHAR',
'compare' => '='
)
)
this array here
array(
'key' => 'wpcf-opredeneno_neopredeleno_vraboten',
'value' => $dogovor,
'type' => 'CHAR',
'compare' => '='
)
is from the post type contracts
so if in the filter i choose full time or part time i can only list the employees name and it does not give me the other data..
i hope it makes sense
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',
),
)
);
}
I want to make a query for country, male and females.
But i don´t know why, the Query always does not recognize that i requested an exact search.
When i query for males it always also querying (fe)males.
$args_members = array(
'numberposts' => -1,
'post_type' => 'members',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'country',
'value' => $country_site->term_id,
'compare' => 'LIKE'
),
array(
'key' => 'gender',
'exact' => true,
'value' => '"˙.$gender_query.˙"',
'compare' => 'LIKE'
),
)
);
$the_query_members = new WP_Query( $args_members );
I also tried a couple of variations manually:
'value' => $gender_query,
'value' => '"male"',
'value' => 'male',
Any Ideas?
Its because you query with compare 'LIKE'... and male LIKE female is true actually. Change 'compare' => 'LIKE' to 'compare' => '='
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
Hi in my post_type=shop i have 2 meta keys and array of values
Custom fields
Name Values
cu_status pending,processing,completed
cu_date 12-Jan-2016 , 13-Jan-2016, ...... any date in the same format date("d-M-Y")
Now i need to loop through all posts with cu_status =pending,processing and cu_date is between 12-Jan-2016 to 13-Apr-2016
What will the query ?
Iam very confused . For to get all post with status pending,processing I know the query
$args = array(
'post_type' => 'shop',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'cu_status',
'value' => array('pending','processing'),
'compare' => 'IN',
)
),
'posts_per_page' => -1
);
Pleases help to complete the query .
You need to use the relation operator e.g.
$args = array(
'post_type' => 'shop',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'cu_status',
'value' => array('pending','processing'),
'compare' => 'IN',
) ,
array(
'key' => 'cu_date',
'value' => array($start, $end),
'compare' => 'BETWEEN',
'type' => 'DATE'
)
),
'posts_per_page' => -1
);
Also use the compare=>BETWEEN for getting the difference in 2 dates.
You may need to tweak the code a bit as I have not tested it.