WP_Query / ACF Custom Fields find more than the exact match - wordpress

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' => '='

Related

Wordpress Custom Post type and post type child filter

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

Custom category and meta key search in wordpress

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.

WP_Query - Load delay with multiple meta_query

I have a Wordpress site that uses quite a few filters to display jobs.
The query will filter jobs by:-
The Job Type
The Location
Job Sector
If it is an International Job
Will check the salary between two values
The current query I am running is:-
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page'=> -1,
'post_type' => 'jobs',
'order' => 'DESC',
'posts_per_page' => 20,
'paged' => $paged,
's' => $search_field,
'meta_query' => array(
'relation' => 'AND',
array(
array(
'key' => 'job_type',
'value' => $job_type,
'compare' => 'LIKE',
),
array(
'key' => 'job_location',
'value' => $job_location,
'compare' => 'LIKE',
),
array(
'key' => 'job_sector',
'value' => $job_sector,
'compare' => 'LIKE',
),
array(
'key' => 'international_job',
'value' => $international_job,
'type' => 'numeric',
'compare' => 'IN',
),
array(
'key' => 'job_location',
'value' => $location_array,
'compare' => 'NOT IN',
),
),
array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'anual_to_salary',
'value' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
array(
'relation' => 'AND',
array(
'key' => 'anual_from_salary',
'value' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
array(
'relation' => 'OR',
array(
'key' => 'job_negotiable',
'value' => 'yes',
'compare' => '=',
),
),
),
)
);
$fetch_jobs = new WP_Query( $args );?>
I'm assuming this is a bad way to do this kind of query as it's taking around 5 - 10 seconds to display any results.
Can anyone recommend a better way to do this type of query with multiple filters?
Thanks in advance.
First of all Your code is like a charm, there is no better way to execute wp_query();. it is taking 5 to 10 second because there is lot of conditions to check and it totally depends on the data on which it have to perform filtration.
I saw whole code and just need a little change that you pass same argument twice i.e. posts_per_page please correct it. else it is the best to perform wp_query.

Multiple values in meta_key query

I have a variable that looks like this
$the_vacancies_industry_areas_list = 'Call Centre','Child Care','Cleaning'
And i want to find if just one of these variables satisfies this case in a meta query
array(
'key' => 'industry_areas_list',
'value' => $the_vacancies_industry_areas_list,
'compare' => 'LIKE',
),
It only seems to satisfy if all three areas are present, not just one.
I want someone with just 'Child Care' or 'Cleaning' for example.
You need to set the relation type on the wp_query args to OR, then define each of the OR conditions as arrays.
$args = array(
'meta_query' => array(
'relation' => 'OR',
array (
'key' => 'industry_areas_list',
'value' => 'Call Centre',
'compare' => 'LIKE'
),
array (
'key' => 'industry_areas_list',
'value' => 'Child Care',
'compare' => 'LIKE'
),
array (
'key' => 'industry_areas_list',
'value' => 'Cleaning',
'compare' => 'LIKE'
)
);
Try this: I made $the_vacancies_industry_areas_list as array variable and pass into wp_query
$the_vacancies_industry_areas_list = array('Call Centre','Child Care','Cleaning');
$args = array(
'post_type' => 'YOUR_POST_TYPE', //Set your post_type
'post_status' => 'publish', //Set your post_status
'meta_query' => array(
array(
'key' => 'industry_areas_list',
'value' => $the_vacancies_industry_areas_list,
'compare' => 'IN'
),
),
);

meta_query search using relation AND returns no result

I have two meta values price and rooms, and need to get post with this two fields, but when I use 'relation' => 'AND', it does not work. If I change it to OR it works fine.
$args = array(
'meta_query'=> array(
'relation' => 'AND',
array(
'key' => 'price',
'compare' => '<=',
'value' => intval($_GET['price']),
'type' => 'numeric',
),
array(
'key' => 'rooms',
'value' => intval($_GET['rooms'])
)
)
);

Resources