Support » Plugins » Hacks » Display list of taxonomies, but only if other taxonomy is specific.

  • Resolved Adam


    I have a custom post-type that has 2 custom taxonomies.
    I want a list one of those taxonomies (containing posts), but only if the other taxonomy for the same post is set to a specific value.

    What I have is:

    <?php $taxA= get_terms("taxA"); ?>
    <?php foreach ($taxA as $A): ?>
       <?php echo $A->name; ?>
    <?php endforeach; ?>

    Suppose I have 3 posts as follows:

    Post-1 : TaxA = “alpha”, TaxB = “green”
    Post-2 : TaxA = “beta”, TaxB = “red”
    Post-3 : TaxA = “gamma”, TaxB = “green”

    The above code will list: alpha, beta, gamma. But I only want to list when TaxB is green, so the list should be: alpha, gamma.

    I hope this makes sense? Does anyone know how to do this?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz


    I don’t think this is the most efficient way to do this, but it’s the one that comes to mind first. Since the posts are the common denominator, I would use get_posts() to get an array of your custom post types that belong to TaxB and have a green value.

    I would then loop through the resulting posts, using wp_get_post_terms() to get the TaxA values if they exist and push each into an accumulator array if the value is not already in the array. Once complete, you have a list of TaxA values meeting your criteria in an array, you can sort it if needed and output the HTML or whatever needs doing.

    I’m not currently in a good position to handle follow up questions, so I held off answering, hoping someone else would take interest. By now, you’ve either figured it out or you will accept any help, so here I am. I’ll try to check back if I can, but either way, good luck.

    Thanks, I did figure it out myself and forgot about this post. I did exactly as you say, iterating through all posts using wp_get_post_terms().
    Sorry again for leaving the question open and thanks for the response.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display list of taxonomies, but only if other taxonomy is specific.’ is closed to new replies.