Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to do a tax-query on a hierarchical taxonomy with no tags. I want to show results for the dealer state and brand being viewed by the user. For instance dealers in texas that also carry honda. I only want to show the results if the meta key value of wpcf-paid is 1 for the dealers.

I have the below code, but it does not seem to work. Any suggestions?

Thank you!

$args = array(
'post_type'=>'dealers',
'meta_key'=>'wpcf-paid',
'meta_value'=> '1',

'tax_query' => array(
'relation' => 'AND',
        array(

            'taxonomy' => 'dealerstate',
            'field' => 'id',
            'terms' => 'terms' => get_terms( 'dealerstate', array( 'fields' => 'ids' ) ),
            'operator'=> 'AND'
        ),

        array(
            'taxonomy' => 'brands',
            'field' => 'id',
            'terms' => 'terms' => get_terms( 'brands', array( 'fields' => 'ids' ) ),
            'operator'=> 'IN'
        )
    )
);

query_posts( $args );

share|improve this question
add comment

2 Answers

You have

'terms' => 'terms' => get_terms( 'brands', array( 'fields' => 'ids' ) ), 

when it should be

'terms' =>  get_terms( 'brands', array( 'fields' => 'ids' ) ),

Unless that is a typo when adding your code example?

share|improve this answer
add comment

I noticed your your meta key is 'wpcf-paid' which would suggest you're using Types to create custom fields. If I'm wrong, disregard the rest of this message...

I had a very similar query using a value from a custom field checkbox created with Types where I also just needed to retrieve the 1 or 0.

Since Types is reluctant to make things easy outside the loop I ended up going to the database and looking up the value generated when the checkbox was checked. The final query came out like this:

'meta_key' => 'wpcf-display-product',
'meta_value' => 'a:1:{s:64:"wpcf-fields-checkboxes-option-3ec42d28a86c01314fe62cdd39e5f584-2";s:1:"1";}',

Hacky and can probably be improved ( this code is from yesterday and looking for a cleaner solution is on my todo list ) but I just wanted to point out that a meta_value => 1 doesn't work with Types ( unless it's a text field of course ).

If you're wondering then you can find the value generated by doing a sql search in your DB:

SELECT * FROM `wp_postmeta` WHERE `meta_key` = 'wpcf-paid'

Remember to change the table prefix if needed.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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