1

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...

3

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.

| improve this answer | |
0
  $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);
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.