Abdul Razzaq
Abdul Razzaq

Reputation: 1

How to get terms or taxonomies only for custom post types

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

Upvotes: 0

Views: 5234

Answers (3)

phpdev
phpdev

Reputation: 179

For retrieving the categories need a taxonomy. You must use the taxonomy without taxonomy can not get term details.

Upvotes: 0

phpdev
phpdev

Reputation: 179

Try with this code to get categories of post type product -

$args = array(
 'post_type' => 'product',
 'taxonomy'  => 'product_cat'
);
$categories = get_terms( $args );

Upvotes: -1

Ivan Frolov
Ivan Frolov

Reputation: 476

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

Upvotes: 0

Related Questions