• Hello all, hoping someone can help with this, and i hope it makes since 🙂

    I found this code and it seems to work to allow users to get search results after selecting multiple tags…

    
    <form method="get" action="<?php bloginfo('url'); ?>">
    <fieldset>
    
    <input type="hidden" name="s" value="" placeholder="search…" maxlength="50" required="false" />
    
    <p>Refine search to posts containing chosen tags:</p>
    
    <?php
    // generate list of tags
    
    $tags = get_tags();
    foreach ($tags as $tag) {
    
    echo
    
        '<label>',
        '<input type="checkbox" name="tag[]" value="',  $tag->slug, '" /> ',
        $tag->name,
        "</label>\n";
    
    }
    ?>
    
    <button type="submit">Search</button>
    </fieldset>
    </form>
              

    However I would like to limit it to tags that are listed under a specific category when viewing the category achieve page…. I found this bit of code (see below) that will display the tags used in post under that category, but it only lets you select one at a time… can anyone help me integrate the two?

        <?php
    				   
                        query_posts('category_name=recipes');
                        if (have_posts()) : while (have_posts()) : the_post();
    
                            if( get_the_tag_list() ){
                                echo $posttags = get_the_tag_list('',' ',' ');
                            }
    
                        endwhile; endif; 
    
    					 wp_reset_query(); 
                    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You cannot use get_the_tag_list() to reliably generate a series of checkbox fields as it only returns tag names, not slugs. A string of names isn’t that useful anyway. You want an array of data that can be looped through.

    I’m not exactly sure which tags you want listed. Is it only tags that are assigned to posts on the current page? If so, you’d need to collect the tags of each post as the posts are output, ensuring there are no duplicates by verifying each tag is not already in the list before adding. For ease in checking it’d be best to compile a list as an associative array in the form of [‘tag slug’ => ‘tag name’,]. Presence can then be easily checked with array_key_exists().

    The problem comes about if your search form occurs before the posts are output. The post query results are already available when the search form is output, but they’ve not yet been output. You could loop through the posts without generating output just to collect the associated tags. The posts are available in the global $wp_query object. After looping through the posts, call $wp_query->rewind_posts() to reset the loop pointer back to the start.

    Thread Starter LōcStar

    (@oglocstar)

    thanks for responding, sorry it has taking me a while to get back… i am wanting to use this code when viewing a certain category such as reciipes, and i want it to output all tags associated with that category which be tags such as chicken, beef, vegetables

    the code above w/o the check boxes does this but your only allowed to select the one tag at a time… i am looking for a way to make it where they can select multiple tags to show all post in the category with selected tags…. the check box code allows me to do this however it shows all tags from the site

    Moderator bcworkz

    (@bcworkz)

    The function get_the_tag_list() gets tags assigned to a single specific post. Collectively you eventually get all tags assigned to posts on a category page, but such output is not useful in a checkbox form, nor is a category page necessarily all posts in a category (there could be multiple pages with other tags).

    I previously described a way to get all tags on a page that are in a useful format. If a post query were expanded to get all posts in a category, not just one page’s worth, you would get all tags used for the category. Such a process is not very efficient. It doesn’t scale well, but it might suffice for your needs.

    At large scale, the results ought to be cached to avoid re-running the process every time you want the form output.

    Why try to relate a category to tags, which has to be done through posts? Why not create child categories (chicken, beef, vegetables) under the principle category (recipes)? They are then directly related and a query for relations is much more efficient.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘selecting multiple tags’ is closed to new replies.