0

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.

4
  • 1
    Try replacing "showposts" with "posts_per_page" Commented Sep 5, 2013 at 15:51
  • Tried that, didn't change anything. Thanks for the tip though.
    – willbeeler
    Commented Sep 5, 2013 at 15:56
  • Try adding 'operator' => 'IN' Commented Sep 5, 2013 at 16:24
  • I had that in there earlier, it didn't do anything for me.
    – willbeeler
    Commented Sep 5, 2013 at 17:48

1 Answer 1

1

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);
16
  • Thanks cybnet, I tried that, but no dice.
    – willbeeler
    Commented Sep 5, 2013 at 15:57
  • Read again the answer, I added more info
    – cybmeta
    Commented Sep 5, 2013 at 15:58
  • Nope, still not producing anything. Let me try something real fast.
    – willbeeler
    Commented Sep 5, 2013 at 16:00
  • Nope, nevermind my Edit to my question, where I ask about location of what I'm trying to do.
    – willbeeler
    Commented Sep 5, 2013 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
    Commented Sep 5, 2013 at 16:14

Your Answer

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

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