WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display list of taxonomies, but only if other taxonomy is specific. (3 posts)

  1. Umbercode
    Member
    Posted 1 year ago #

    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?

  2. bcworkz
    Member
    Posted 1 year ago #

    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.

  3. Umbercode
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.