Reputation: 4323
I have registered a custom taxonomy as part of my custom post type, but when passing it through to get_categories()
it returns an empty array. Any ideas as to why?
// Register FAQ Categories taxonomy
function bv_faq_register_categories() {
register_taxonomy(
'faq-category',
'faq',
array(
'label' => 'Categories',
'rewrite' => array('slug' => 'faq-category'),
'hierarchical' => true
)
);
}
add_action('init', 'bv_faq_register_categories');
// Category view
$categories = get_categories(array(
'taxonomy' => 'faq-category'
));
$categories
is returning an empty array.
Upvotes: 0
Views: 1268
Reputation: 172
Your code looks ok. Do you have assigned this category to any post/post type?
If not then you will get an empty result. for testing you can set 'hide_empty' = false
like this:
// Category view
$categories = get_categories(array(
'taxonomy' => 'faq-category',
'hide_empty' => false // set it true
));
Also, you can use get_terms()
function.
Upvotes: 0
Reputation: 566
Like @AD Styles said I would use get_terms using the custom taxonomy, to expand a bit here's some example code:
<?php
$post_type = 'faq-category';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( array(
'taxonomy' => $taxonomy,
'parent' => 0
) );
foreach( $terms as $term ) :
echo "<h1>".$term->name."</h1>";
endforeach;
endforeach;
?>
Upvotes: 0
Reputation: 11
Have you tried get_terms instead?
$categories = get_terms( 'faq-category', array(
'orderby' => 'count',
'hide_empty' => 0
) );
Upvotes: 1