How to get all Existing Categories from a Custom Post Type

Question

If you know the taxonomy slug of it you can use code below:

$tax_slug = 'product_cat';
$cpt_terms = get_term( $tax_slug );
echo '<pre>';
  print_r( $terms );
echo '</pre>';

but if you don’t know the slug, you can use these method:
First click on the taxonomy you want to see its terms. In the URL bar in your browser you find sth like this:

http://localhost/wordpress/wp-admin/edit-tags.php?taxonomy=product_cat&post_type=product

you can find taxonomy slug and use it in the above code.

Second use code below to find taxonomies of a Post Type:

$cpt_tax = get_object_taxonomies( array( 'post_type ) => 'product' );
echo '<pre>';
  print_r( $cpt_tax);
echo '</pre>';

Note: In this example I used ‘product’ as post type slug. if you don’t know the slug you can find it by clicking on all posts of your CPT and in the URL bar of your browser you find this:

http://localhost/wordpress/wp-admin/edit.php?post_type=product

0
agent abstract 2 months 2022-12-20T13:07:38-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse