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 );