0

First time using wordpress query so not sure how to handle this problem. I want to get post titles where meta_key is 'tag' and meta_values are 'A, B, C'. Something similar to IN() function of sql query. How should I do to achieve that?

This is my code so far but it is not working as it ignores condition.

 //$tags consists of my meta_value ( A, B, and C) 
    foreach ($tags as $input) {


    $data[] = array( 'meta_key' => 'tag', 'meta_value' => $input, 'orderby' => 'post_date' ); 


    }




        $query = new WP_Query( $data );

Thanks in advance for your kind help.

1
  • Post with more information about the query you want to build. Don't worry about using WordPress, just use words or psuedo SQL if you know that. Commented Jul 13, 2011 at 1:38

2 Answers 2

2

Read the Custom Field Parameters section on the WordPress Codex for WP_Query.

The problem is you are rebuilding your entire $data array on each loop. This passes invalid parameters to WP_Query. So I imagine your getting undefined results.

You simply want to set meta_value directly or specify a meta_query parameter similar to the IN clause you described.

Sign up to request clarification or add additional context in comments.

5 Comments

Well, thx for the comment. I will like to set the meta_value directly but the amount of my data is dynamic so I can't do it that way. As for meta_query, I am not too sure how to do it. Can you kindly show me an example? Thanks.
Hi Jason, after a few tries, I managed to get it done using meta_query. Thank you so much for your help.
Great. Glad the link helped. I would encourage you to either comment back with the solution or answer your own question for the next passer-by.
As you suggested, I have posted the solution I got. I should have read more clearly the documentary before posting. What a dumb ques I ask here. Nevertheless, thanks for your help despite being asked a stupid ques. LOL
No worries. You were figuring something out. That's the whole point of the site.
1

Here is the answer:

$args = array(

    'meta_query' => array(
    array(
        'key' => 'tag',
        'value' => $tags,
        'compare' => 'IN'
        ) 
    ),

    'orderby' => 'post_date'    

);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.