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.

This has been driving me nuts for hours. I've checked numerous pages on the web for an answer but I can't find one. I'm trying to get all of the posts from a custom post type, that are attached to an array of taxonomy ids. Here's the code

        $args = array( 
            'post_type' => 'contractors',
            'showposts' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => $taxonomy,
                    'terms' => $taxarray,
                    'field' => 'id'
                )
            )
        );
    $query = new WP_Query($args);

If you're wondering about the variables...

$taxonomy="service_type"; //and yes, this exists
$taxarray = array(24); //and yes, this id exists, and yes it has custom post types attached.

Basically, when I print_r($query), no posts are returned. Of course, I see all the other junk that spits out of it, but there's no posts. I'm pulling my hair out over this.

EDIT: I don't know if this is helpful, but a couple of things should be noted. I'm trying to do this inside a custom class->function, and I'm on the latest wordpress, and php 5+.

EDIT #2: New pastebin with fuller code: http://pastebin.com/px9N4LCV

FINAL EDIT: I figured out why it wasn't working. I was running the query too early, and Wordpress wasn't recognizing my taxonomy yet.

share|improve this question
1  
Try replacing "showposts" with "posts_per_page" –  Vinod Dalvi Sep 5 '13 at 15:51
    
Tried that, didn't change anything. Thanks for the tip though. –  willbeeler Sep 5 '13 at 15:56
    
Try adding 'operator' => 'IN' –  Vinod Dalvi Sep 5 '13 at 16:24
    
I had that in there earlier, it didn't do anything for me. –  willbeeler Sep 5 '13 at 17:48

1 Answer 1

up vote 1 down vote accepted

I think showposts was replaced by posts_per_page in wordpress 2.1. See pagination parameters. Also note that print_r($query) won't return any results because $query variables is WP_Query object; you need to exceute the query.

$args = array( 
        'post_type' => 'contractors',
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => $taxonomy,
                'terms' => $taxarray,
                'field' => 'id'
            )
        )
    );
$query = new WP_Query($args);
$results = $query->get_posts();
print_r($results);
share|improve this answer
    
Thanks cybnet, I tried that, but no dice. –  willbeeler Sep 5 '13 at 15:57
    
Read again the answer, I added more info –  cybmeta Sep 5 '13 at 15:58
    
Nope, still not producing anything. Let me try something real fast. –  willbeeler Sep 5 '13 at 16:00
    
Nope, nevermind my Edit to my question, where I ask about location of what I'm trying to do. –  willbeeler Sep 5 '13 at 16:07
    
Can you update your question with your full code and some more iformation like where are you executing the code. I've tested the code I posted and it works perfectly. –  cybmeta Sep 5 '13 at 16:14

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.