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!
Related
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 would like to retrieve all those posts whose author (post table field) is a given one OR those which has a given meta value (postmeda table field).
If "author" was a meta value, I know I could use a meta_query to achieve it. The thing here is that it is not... so I think I cannot use the "author" field within a meta_query and use the "relation" key.
I'm looking for something like:
$args = array(
'post_type' => array('post'),
'orderby' => 'ASC',
'order' => 'date',
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'field' => 'author',
'value' => $author_id,
'compare' => '==',
),
array(
'key' => '_meta_field_name',
'compare' => 'NOT EXISTS',
),
),
array(
'relation' => 'AND',
array(
'key' => '_meta_field_name',
'compare' => 'EXISTS',
),
array(
'key' => '_meta_field_name',
'value' => $meta_field_value,
'compare' => '==',
),
),
),
);
$data = new WP_Query( $args );
Any suggestion on how to achieve that using WP_Query?
Thanks!
You might like to try an approach like this one instead. The idea is to query the two conditions you want to search for, then merge the two queries into one finished product.
//Get posts with the author you're looking for
$args1 = array(
'author_name' => 'testuser', //or 'author' => $author_id or something else
);
$data1 = get_posts( $args1 );
//Get posts with the meta data you're looking for
$args2 = array(
'meta_query' => array(
array(
'key' => 'meta_field_name',
'compare' => 'EXISTS',
),
array(
'key' => 'meta_field_name',
'value' => $meta_field_value,
'compare' => '==',
),
),
);
$data2 = get_posts( $args2 );
//Merge both arrays
$allData = array_merge( $data1, $data2 );
//Get just the IDs of all the posts found, while also dropping any duplicates
$postIDs = array_unique( wp_list_pluck( $allData, 'ID' ) );
//Do a new query with these IDs to get a properly sorted array of post objects
$args3 = array(
'post__in' => $postIDs,
'order' => 'ASC',
'orderby' => 'date',
);
$finalAnswer = get_posts( $args3 ); //This is your array of post objects. Ta-Da!
I'm having issues with my WP_Query. So I have two select boxes created using ACF one for Country and one for sector. When I select country and leave sector empty I get all the posts with the selected country and like wise for the sector. What I need to do is filter it further so if both are selected and not just one I get all posts from that country and sector. My args below any help is appreciated.
$field_key_country = "field_57b4439b5e371";
$field_country = get_field_object($field_key_country);
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : false;
$sector_key = "field_57e15152d896d";
$sector_field = get_field_object($sector_key);
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : false;
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'dis_country',
'value' => $country_sector,
),
array(
'key' => 'dis_sector',
'value' => $sector,
)
)
);
May be it's helpful for you -
$field_key_country = "field_57b4439b5e371";
$field_country = get_field_object($field_key_country);
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : false;
$sector_key = "field_57e15152d896d";
$sector_field = get_field_object($sector_key);
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : false;
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1
);
if($country_sector){
$args['meta_query'][] = array(
'key' => 'dis_country',
'value' => $country_sector
);
}
if($sector){
$args['meta_query'][] = array(
'key' => 'dis_sector',
'value' => $sector
);
}
Please try following code with relation AND & compare operator.
Also please check WP_Query class at https://codex.wordpress.org/Class_Reference/WP_Query
<?php
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : '';
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : '';
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'dis_country',
'value' => $country_sector,
'compare' => '=',
),
array(
'key' => 'dis_sector',
'value' => $sector,
'compare' => '=',
)
)
);
?>
'distributer_search',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'dis_country',
'value' => $country_sector,
'compare' => '=',
),
array(
'key' => 'dis_sector',
'value' => $sector,
'compare' => '=',
)
)
);
?>
I create a query like that:
<?php
$args = array(
'numberposts' => -1,
'post_type' => 'product',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'pa_color',
'value' => array('red'),
'compare' => 'IN',
),
array(
'key' => 'pa_for',
'value' => 'Men',
'compare' => '=',
),
),
);
$post = get_posts($args);
echo '<pre>';
print_r($post);
echo '</pre>';
When I try to run this code I get nothing. Please help me check this query and how do I get products by attribute and price?. I take meta_key in term_taxonomy table. Many thanks!
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'
)
)
);