0

I have a custom taxonomy(the taxonomy is types) that is basically a selector that tells WordPress which section of the site to place the new post.

So for example

  1. I create a new post and select Lifestyle from the custom taxonomy list.
  2. Then I would select a category to place the post under, for example Healthy

I am trying to list the categories of Lifestyle in a custom menu format but only if posts exist under the taxonomy type and category selected.

What I am getting is a full list of categories but not filtered by taxonomy type.

Can anyone see where I have gone wrong?

<?php
$args=array(
  'types' => 'Lifestyle',
  'orderby' => 'name',
  'order' => 'ASC',
  'hide_empty' => 1
  );
$categories=get_categories($args);
  foreach($categories as $category) {
    if ($category->count > 0){
    echo '<li class="'.$category->slug.'"><a href="?category=' . $category->slug . '">'.$category->name.' / '.$category->count.'</a></li>';
        }
    } 
?>

Thanks so much in advnce! -d

2

get_categories() only lists categories, so you shouldn't be seeing any terms from the custom taxonomy. What you want to use is get_terms() which takes a taxonomy as an argument. As you can read about in this answer, get_terms() is actually used by get_categories(), it just prefills the "taxonomy" argument with "category."

If you swap in get_terms() and add the name of your custom taxonomy, your code looks like it should work. The only other thing you may be able to change is your if statement checking the category count. If you're already doing 'hide_empty' => 1 you shouldn't need that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.