using multiple meta_key and meta_value in query_posts
-
21-04-2021 - |
Question
how can i use multiple meta_key and meta_value in query_posts?
For example, I want to find multiple content with two different meta_key and meta_value. How do I do this?
i using this code but not effective:
query_posts('meta_key=test2&meta_value=hello&meta_key=test2&meta_value=bye');
please help me...
Solution
This is not currently possible with query arguments alone (realm of filtering raw SQL query and such).
If your task is not time-critical then I suggest to wait for upcoming WP 3.1 release. It will feature much more flexible querying capabilities for custom fields.
See Advanced Metadata Queries for post on upcoming improvements.
OTHER TIPS
$args = array(
'post_type' => 'custompost_type',
'posts_per_page' => '1',
'meta_query' => array(
'relations' => 'AND', // you can use OR also as your requirement
array(
'key' => 'serial-number',
'value' => '123'
),
array(
'key' => 'model-number',
'value' => '456'
),
),
'post_status' => 'publish',
);
$posts = get_posts($args);
if(sizeof($posts) > 0){
var_dump($posts);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange