0

My question is about retrieving custom post types via get_posts, when custom post types also have a custom taxonomy. I can use get_posts to retrieve all posts of a custom post type, but when I attempt to filter posts by category, I get no results.

Therefore, I think that there is either something wrong with 1) the way I am registering the custom taxonomy for the custom post type, or 2) that I have categories and taxonomy confused, or both.

To retrieve random posts of the custom post type "books" my code looks like this:

$posts = get_posts(array(
    'numberposts' => -1,
    'orderby' => 'rand',
    'post_type' => 'books'
));

if($posts)
  {
     foreach($posts as $post)
    {
    // output various things 
    }

  }

However if in the list of parameters I add

'category' => 3,

... nothing shows up. And I know there are "books" with a tag or category of 3 assigned to them and that the tag_id = 3. When I hover over the term in the custom taxonomy area I get this as its link:

http://ourwebsite.ca/wp-admin/edit-tags.php?action=edit&taxonomy=books_taxonomy&tag_ID=3&post_type=books

This seems to me like it should work perfectly unless categories and custom post taxonomy are two different things. But, if they are, I don't know how to pull custom posts based on taxonomy.

Here is how I've registered the custom taxonomy for "books"

function books_taxonomy() {
   register_taxonomy(
    'books_taxonomy',
    'books',
    array(
        'hierarchical' => true,
        'label' => 'Books Set',
        'query_var' => true,
        'rewrite' => array('slug' => 'books')
        )
    );
}

Is anyone able to advise me on filtering a listing of "books" by a taxonomy term? Thanks so much in advance!

1
  • What taxonomy are you actually using for your CPT: category, post_tag, or books_taxonomy? Commented Oct 29, 2013 at 23:19

1 Answer 1

1

Note that category and post_tag are both taxonomies, and that they are taxonomies that, by default, are only supported by the post post type. You have created a custom post type, and have registered support for a custom taxonomy for it: books_taxonomy.

Thus, it makes perfect sense that a query for posts with the books post-type and the category taxonomy would return no results: because the books post-type does not support the category taxonomy.

Instead, you need to query for your custom taxonomy.

Note that get_posts() is just a wrapper for WP_Query(), and custom queries are often easier to write/use when using WP_Query() directly. So, here is how you would query for your CPT and custom taxonomy:

$books_query_args = array(
    // Post Type
    'post_type' => 'books',
    // Posts per page - note: use instead of numberposts
    'posts_per_page' => -1,
    // Orderby
    'orderby' => 'rand',
    // Taxonomy query
    'tax_query' => array(
        array(
            'taxonomy' => 'books_taxonomy',
            'field' => 'id',
            'terms' => array( 3 )
        )
    )
);
$books_query = new WP_Query( $books_query_args );

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.