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:
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!
category
,post_tag
, orbooks_taxonomy
?