skip to Main Content

I have an array of post types and I want to fetch all categories or terms which only belong to these post types. Like product post type have a category with the name "product_cat" I used get_terms() but it returns all available terms. If I use get_categories() It returns only terms of post type.

$post_types = array('post', 'product', 'page');

3

Answers


  1. You can to get it next – example of getting all custom taxonomy brand_categories of custom post brand

    $args = array(
      'post_type' => 'brand',
      'taxonomy'  => 'brand_categories'
    );
    $categories = get_terms( $args );
    
    Login or Signup to reply.
  2. Try with this code to get categories of post type product –

    $args = array(
     'post_type' => 'product',
     'taxonomy'  => 'product_cat'
    );
    $categories = get_terms( $args );
    
    Login or Signup to reply.
  3. For retrieving the categories need a taxonomy.
    You must use the taxonomy without taxonomy can not get term details.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search