简体   繁体   中英

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM