1

So I have a page template that is setup as follows (using Kleo theme):

Search template

get_header(); ?>


<?php
//create right sidebar template
kleo_switch_layout('right');
?>

<?php get_template_part('page-parts/general-title-section'); ?>

<?php get_template_part('page-parts/general-before-wrap'); ?>

<?php
if ( have_posts() ) :
    // Start the Loop.
    while ( have_posts() ) : the_post(); ?>

<!-- Begin the Treeview menu -->
<form method="get" action="<?php bloginfo('url'); ?>">
    <div class="form-group">
        <input class="form-control" type="text" name="s" value="" placeholder="Search…" maxlength="50" required="required" />
    </div>
    <p>Refine search to posts containing chosen tags:</p>
<div class="acidjs-css3-treeview">
    <ul>
        <li><input type="checkbox" id="node-0" /><label><input type="checkbox" name="tag[]" value="node-0" /><span></span></label><label for="node-0">node-0</label>
            <ul>
                <li><input type="checkbox" id="node-0-0" /><label><input type="checkbox" name="tag[]" value="node-0-0" /><span></span></label><label for="node-0-0">node-0-0</label>
                    <ul>
                        <li><input type="checkbox" id="node-0-0-0" /><label><input type="checkbox" name="tag[]" value="node-0-0-0" /><span></span></label><label for="node-0-0-0">node-0-0-0</label></li>
                        <li><input type="checkbox" id="node-0-0-1" /><label><input type="checkbox" name="tag[]" value="node-0-0-1" /><span></span></label><label for="node-0-0-1">node-0-0-1</label></li>
                    </ul>
                </li>
            </ul>
        </li>
        <li><input type="checkbox" id="node-1" /><label><input type="checkbox" name="tag[]" value="node-1" /><span></span></label><label for="node-1">node-1</label>
            <ul>
                <li><input type="checkbox" id="node-1-0" /><label><input type="checkbox" name="tag[]" value="node-1-0" /><span></span></label><label for="node-1-0">node-1-0</label>
                    <ul>
                        <li><input type="checkbox" id="node-1-0-0" /><label><input type="checkbox" name="tag[]" value="node-1-0-0" /><span></span></label><label for="node-1-0-0">node-1-0-0</label></li>
                        <li><input type="checkbox" id="node-1-0-1" /><label><input type="checkbox" name="tag[]" value="node-1-0-1" /><span></span></label><label for="node-1-0-1">node-1-0-1</label></li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
</div>
<!-- End the Treeview menu -->
<input class="btn btn-primary" type="submit" value="Submit" />
</form>

        <?php
        /*
         * Include the post format-specific template for the content. If you want to
         * use this in a child theme, then include a file called called content-___.php
         * (where ___ is the post format) and that will be used instead.
         */
        get_template_part( 'content', 'page' );
        ?>

        <?php get_template_part( 'page-parts/posts-social-share' ); ?>

        <?php if ( sq_option( 'page_comments', 0 ) == 1 ): ?>

            <!-- Begin Comments -->
            <?php comments_template( '', true ); ?>
            <!-- End Comments -->

        <?php endif; ?>

    <?php endwhile;

endif;
?>

<?php get_template_part('page-parts/general-after-wrap'); ?>

<?php get_footer(); ?>

Problem

The search is not functioning properly. It returns a query string as such:

The URL would show http://example.com/?s=searchterm&tag[]=key-word1&tag[]=key-word2

This would result in the tags not being filtered.

I can edit the [] out to make the tag search work, but then I would only be searching the last tag element in the GET, which defeats the purpose.

I was inspired to do this particular code for searching multiple tags on this question here. That link seems to suggest this won't work until WP 4.4 comes around. Any way of getting this working under current WP 4.3.1?

2
  • Have you tried this with a bundled theme-- one of the Twenty-Something ones? Your theme, or a plugin, could be breaking functionality.
    – s_ha_dum
    Commented Nov 8, 2015 at 22:45
  • @s_ha_dum - I have just tested it out with 2015 and same problem. Commented Nov 8, 2015 at 22:59

2 Answers 2

1

Well, I got it working by making my own own parse php as follows:

parse.php

<?php

$tags = $_POST['tag'];
$search = $_POST['s'];
$count = count($tags);

$i = 0;
if(!empty($search))
    $uri = "https://example.com/?s=$search&";
else
    $uri = "https://example.com/?tag=";
foreach($tags as $name=>$value) {
    ++$i;
    if($i !== $count)
        $uri .= $value."+";
    else
        $uri .= $value;
}

header("Location: $uri");
exit;
?>

And of course, changing the original form action to point to parse.php and the method to POST.

Probably not the best way of doing this, but for now it works. If someone can give me a better or cleaner answer, please do.

1
  • could you post a link to website which uses the multiple tags search? I'd like to see it in action and would like to add such feature to my blog too
    – Dmitriy
    Commented Nov 19, 2017 at 17:26
0

I'm having the same problem. After going through hooks (pre_get_posts doesn't do the job) I decided to check for $SERVER in the template_include hook. substr($_SERVER['REQUEST_URI'],1) is the query string that we can parse. In my setup, tags are separated by a +sign in the pretty URL. All I do is str_replace that with ',' and feed it into, alas, a fresh query_post().

This works at least with an URL like {domain}/cats+dogs+donkeys by just filtering out any instance of + in the URL and doing a fresh query.

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.