1

I am looking to display all tags in select form that are from posts that have been assigned to a specific category.

I am using the following code to generate every tag in a select form

<div>
<?php
echo "<select onChange=\"document.location.href=this.options[this.selectedIndex].value;\">";
echo "<option>By product</option>\n";
foreach (get_tags() as $tag)
{
  echo "<option value=\"";
  echo get_tag_link($tag->term_id);
  echo "\">".$tag->name."</option>\n";
}
      echo "</select>"; ?>
</div>

Can someone point me in the direction on how I can just display the tags that are from all posts assigned to the videos category?

Any help much appreciated

2
  • what error you are getting or what output you are getting out of it
    – Domain
    Commented Oct 16, 2015 at 13:53
  • I am getting a list of all tags. I want to display all tags that are from posts that have been assigned to the specific category
    – rikardo85
    Commented Oct 16, 2015 at 14:01

2 Answers 2

2

From my understanding of the question and your comment:

I want to display all tags that are from posts that have been assigned to the specific category

You would first need to get all the posts that have that tag assigned, loop through said posts and save the unique tags to an array. Finally, loop through the array and display them in the select list. The only thing you would need to do on your end is replace $reltaed_term_id = 7 with whatever the ID is of the tag you're looking to target.

<?php
    $reltaed_term_id        = 7;
    $unique_related_tags    = array();

    $related = new WP_Query( array(
        'post_type'     => 'post',
        'posts_per_page'=> -1,
        'fields'        => 'ids',
        'cat '          => $reltaed_term_id,
    ) );

    if( $related->have_posts() ) {
        foreach( $related->posts as $post_id ) {
            $tags = wp_get_post_tags( $post_id );
            if( ! empty( $tags ) ) {
                foreach( $tags as $tag ) {
                    if( empty( $unique_related_tags ) || ! array_key_exists( $tag->term_id, $unique_related_tags ) ) {
                        $unique_related_tags[ $tag->term_id ] = $tag->name;
                    }
                }
            }
        }

        wp_reset_postdata();
    }

    if( ! empty( $unique_related_tags ) ) :
?>

        <div>
            <select onChange="document.location.href=this.options[this.selectedIndex].value;">
                <option>By product</option>

              <?php foreach( $unique_related_tags as $tag_id => $tag_name ) : ?>

                <option value="<?php echo get_tag_link( $tag_id ); ?>"><?php echo $tag_name; ?></option>

              <?php endforeach; ?>

            </select>
        </div>

<?php endif; ?>

I haven't tested the above code so let me know if you run into issues.

5
  • 1
    I would just set 'fields' => 'ids' in the post query as you really are just interested in the post ID's. This will save a huge amount of resources ;-) Commented Oct 16, 2015 at 14:37
  • Thanks. Unfortunatly this breaks the site. I already have the required posts on display. The idea is for the user to select a tag from a select field which then directs them to the tags page. The code I've entered above generates all tags and directs the user to whatever is selected. I want to only display all tags from posts that have been assigned to a specific category (videos).
    – rikardo85
    Commented Oct 16, 2015 at 14:39
  • @PieterGoosen Haha I had this in there when writing it up - I guess it got lost in translation somewhere. Thanks!
    – Howdy_McGee
    Commented Oct 16, 2015 at 14:39
  • @rikardo85 - I've fixed the missing bracket issue and it looks to run without error now. You just need to find the Videos Category ID and replace $reltaed_term_id = 7; with whatever that videos ID is.
    – Howdy_McGee
    Commented Oct 16, 2015 at 14:42
  • 1
    Howdy_McGee - Thank you! Exactly what I'm looking for!
    – rikardo85
    Commented Oct 16, 2015 at 15:58
-1
<div>
<?php
echo "<select onChange=\"document.location.href=this.options[this.selectedIndex].value;\">";
echo "<option>By product</option>\n";
foreach (get_tags() as $tag)
{
if($tag['taxonomy']=="your_category"){
  echo "<option value=\"";
  echo get_tag_link($tag->term_id);
  echo "\">".$tag->name."</option>\n";
  }
}
      echo "</select>"; ?>
</div>

use this and put your category

3
  • 1
    To most users, what you have posted is totally useless as no one knows what your code does, how it works ao even where to add it. A good answer should at least have a good explanation (actually, this is compulsary) :-) Commented Oct 16, 2015 at 14:34
  • Unfortunatly this doesnt work. This breaks the site.
    – rikardo85
    Commented Oct 16, 2015 at 14:43
  • My logic was to put condition in loop to show only your category
    – Domain
    Commented Oct 17, 2015 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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