Bumped by Community user
    Bumped by Community user
2 added 16 characters in body
source | link

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

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!

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!

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
source | link

Can't pull posts of a Custom Post Type based on the custom taxonomy of the CPT

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!