Multiple values in meta_key query - wordpress

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

Related

Sort WP_Query using multiple meta_keys not working

I have a custom post type, that has price in 2 different meta keys, based on product type. I want to filter the query so that all products can be displayed in either asc or desc order. Here is my query and it is not sorting the results.
$args = array(
'post_type' => 'ars-products',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'ars_product_price',
'compare' => 'EXISTS',
),
array(
'key' => 'ars_product_min_amount',
'compare' => 'EXISTS',
)
),
array(
'relation' => 'OR',
array(
'key' => 'ars_product_type',
'value' => 'one_time',
'compare' => '=',
),
array(
'key' => 'ars_product_type',
'value' => 'donation',
'compare' => '=',
),
),
),
'orderby' => array(
'ars_product_price' => 'ASC',
'ars_product_min_amount' => 'ASC'
)
);
$products_data = new \WP_Query( $filters );
Edit: it does sort perfectly, if I only use one meta key ars_product_price.

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

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

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.

Wordpress meta_query to compare 3 meta value with same meta key

I would like to fetch all post that have meta_value Book, Study, Art. But when I put 3 array on meta_query system doesn't return anything, but if I put online 2 array It's work
// this working
$args = array(
'numberposts' => 10,
'post_type' => 'content',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'media_type',
'value' => 'Book',
'compare' => 'LIKE'
),
array(
'key' => 'media_type',
'value' => 'Study',
'compare' => 'LIKE'
)
)
);
//this now working
$args = array(
'numberposts' => 10,
'post_type' => 'content',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'media_type',
'value' => 'Book',
'compare' => 'LIKE'
),
array(
'key' => 'media_type',
'value' => 'Study',
'compare' => 'LIKE'
),
array(
'key' => 'media_type',
'value' => 'Art',
'compare' => 'LIKE'
)
)
);
It sounds like you are searching for the exact meta values, so did you try:
$args = array(
'posts_per_page' => 10,
'post_type' => 'content',
'meta_query' => array(
array(
'key' => 'media_type',
'value' => array( 'Art', 'Book','Study' ),
'compare' => 'IN'
),
),
);
as your query arguments?
Sidenote: The parameter numberposts works, but posts_per_page is now more commonly used in WP_Query().

Resources