Support » Fixing WordPress » Let users search for posts that contain all selected tags

  • I’m trying to set up a site with a search function that allows people to select several tags or cats or whatever from a list, and then see only posts that contain ALL of those tags, cats or whatever. I.E., if they choose “dogs, cats, bananas”, they do not see posts that are only tagged “dogs, bananas” – they only see posts with all three.

    I’ve found a lot of plugins and several posts that come oh so very close to the answer, but not quite. I found one closed thread here from three years ago where someone worked hard to write php for this, bu in the end, decided it just wasn’t possible. I’m hoping something has changed.

    I would even be open to a standalone solution that can query the WP database in its own way. That could be incorporated into a WP page, and WP would still be at the heart of everything.

Viewing 7 replies - 1 through 7 (of 7 total)
  • ‘+’ sign will work on url. eg: site.com/tag/cats+dogs+bananas.
    but am confused on how to feed multiple selections into url. guess you will need a JS to handle input name values in array[]? lets see if anybody can add in..

    <form action="<?php get_site_url(); ?>">
    <input type="checkbox" name="tag" value="cats">cats<br>
    <input type="checkbox" name="tag" value="dogs">dogs<br>
    <input type="submit" value="submit">
    </form>

    ok try this:

    add this code to where you want to display tags checkbox:

    <form action="yoursite.com/tagsredirect.php" method="post">
    <?php $tags = get_tags();
    foreach ( $tags as $tag ) {
    echo "<input type=\"checkbox\" name=\"$tag->name\" value=\"$tag->slug\">$tag->name<br />";
    } ?>
    <input type="submit" value="submit">
    </form>

    now create a new file called tagsredirect.php and place it in your webserver (note the full URL to this file and put it in above code).
    add this code to tagsredirect.php file:

    <?php
    $myurl = 'http://yourwebsite.com/tag/'.implode("+",$_POST);
    header("Location: $myurl");
    exit;
    ?>

    thats it. try if this works.

    Note: depending on how many tags you have, the first loop of generating input elements for all tags could be freakishly huge.
    also, hope you are proficient with coding. else, take proper backups and work with a child theme.

    Thread Starter Sapphire

    (@sapphire)

    Wow, you’re quick! Thank you!

    Okay, I finally put together a dummy installation to test this out, and it’s not quite right.

    First, the structure of “site.com/tag/cats+dogs+bananas” brings up a 404 on my site. I discovered that “site.com/?tag=cats+dogs+bananas” works, but it brings up every post that has ANY of those tags.

    I want something that works like Webtender’s “in my bar” feature, which is probably custom code, and definitely not WordPress. People enter what ingredients they have on hand, and it brings up only recipes that can be made with those ingredients.

    In terms of tags, that would mean a script that brings up only recipes that do NOT contain tags OTHER than the ones selected. It’s more like an exclusion filter than a search. I have no idea if WordPress can be made to work like that. :/

    first step first.. you use the ‘?’ if you are not using custom permalinks.
    if ‘,’ acts as an OR gate and ‘+’ as an AND gate. i tried this code on my site and it works. so recheck once more on yours. also check if you are using ‘slugs’ and not names of tags.

    Thread Starter Sapphire

    (@sapphire)

    Aha, I hadn’t set up custom permalinks on the dummy install. Now I have, and it works as an and gate or an or gate.

    However, what I need is different. It’s my fault for failing to understand and communicate it clearly. I guess the simplest way to phrase what I need is: something that EXCLUDES all tags that were NOT selected by the user. Can WordPress do that at all? I’m find with using categories instead of tags or anything that’ll work.

    Taking another look at the Webtender example I shared above, they have a Javascript file which seems to use tags, so it would seem it’s possible to do this with a WP database…. just might require a totally separate script to do it.

    then pls close this thread and start a new one with proper title.

    Thread Starter Sapphire

    (@sapphire)

    I don’t see an option to “close” the thread. I see “resolved”, but that would be misleading.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Let users search for posts that contain all selected tags’ is closed to new replies.