0

I've spent a day or so on this now and I'm turning to the community for assistance.

Firstly, I am trying to achieve something like this post:

Wordpress Multiple Category Search

In my theme template, I have a custom search form:

<form id="searchform" method="get" action="<?php bloginfo('url'); ?>">
<fieldset>

    <input type="text" name="s" value="" placeholder="Search..."></input>
    <button type="submit">Search</button>
    <div class="clear"></div>
    <div class="search_category_section">
    <?php
        $args = array('parent' => 0);
        $categories = get_categories($args);

        echo '<div class="search_category_section">';

        foreach ($categories as $category) {

        $thecatid = $category->cat_ID;
            echo '<div class="categorylist"><div class="parent_category_list_item"><input id="', $thecatid, '" type="checkbox" name="category_name" value="', $category->slug, '"><label for="', $thecatid, '">', $category->name, '</label></input></div>';
            $childcats=get_categories(array('parent' => $thecatid));
                foreach($childcats as $c) {
                echo '<div class="child_category_list_item"><input id="', $c->cat_ID, '" type="checkbox" name="category_name" value="', $c->slug, '"><label for="', $c->cat_ID, '">', $c->name, '</label></input></div>';
            }
        echo '</div>';
        }
    ?>
    </div>
</fieldset>

So, let's say for example I search for "keyword", and I select two checkboxes, "category A" and "category B". I have two posts, one in each category. Each of those two posts has that keyword in it. After hitting the submit button it generates the following URL:

mydomain.com/?s=keyword&category_name=category-a&category_name=category-b

What this is currently doing is presenting me with one result - that is, one post from category B with that keyword in it.

What I'm actually looking for is this result:

mydomain.com/?s=keyword&category_name=category-a,category-b

Whereby I get all posts in all categories, with that keyword in it.

I've looked high and low, and while I've found results where others were trying to achieve a similar thing, none of their solutions worked for me.

This is all part of a learning curve for me. I have found this plugin which looks like it will do what I want, but as you've all probably been in this situation whereby "I wish I could do this myself so that I understand better how the mechanics behind Wordpress" work I hope you'll appreciate the question.

Thanks in advance, if anything isn't clear or I've left vital info out let me know.

1

let’s start by defining an HTML search form:

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

Add this to functions.php

function advanced_search_query($query) {

    if($query->is_search()) {

        // tag search
        if (isset($_GET['taglist']) && is_array($_GET['taglist'])) {
            $query->set('tag_slug__and', $_GET['taglist']);
        }

        return $query;
    }

}
add_action('pre_get_posts', 'advanced_search_query', 1000);
  • Hi Preethi - as it happens I was talking to one of my colleagues who recommended sticking it in an array, and it looks you beat him to it. Cheers for the reply, I will update accordingly. – AmmyKami83 Jul 15 '16 at 9:32
0

Here is what worked for me.

Preethis answer almost got there, but no data was getting set. I needed to find a way of accessing the data in the array, and I used implode to do it.

So here is my final code that enabled me to create a search form that loops and displays categories, allows the user to do a keyword search via any number of categories, and display results IF the keyword exists in any of the categories selected:

I created a searchform.php file in my theme file and included the following HTML:

<form id="searchform" method="get" action="<?php bloginfo('url'); ?>">
<fieldset>

    <input type="text" name="s" value="" placeholder="Search..."></input>
    <button type="submit">Search</button>
    <div class="clear"></div>
    <div class="search_category_section">
    <?php
        $args = array('parent' => 0);
        $categories = get_categories($args);

        echo '<div class="search_category_section">';

        foreach ($categories as $category) {

        $thecatid = $category->cat_ID;
            echo '<div class="categorylist"><div class="parent_category_list_item"><input id="', $thecatid, '" type="checkbox" name="category_name[]" value="', $category->slug, '"><label for="', $thecatid, '">', $category->name, '</label></input></div>';
            $childcats=get_categories(array('parent' => $thecatid));
                foreach($childcats as $c) {
                echo '<div class="child_category_list_item"><input id="', $c->cat_ID, '" type="checkbox" name="category_name[]" value="', $c->slug, '"><label for="', $c->cat_ID, '">', $c->name, '</label></input></div>';
            }
        echo '</div>';
        }
    ?>
        </div>
</fieldset>

and then, my functions.php has the following code:

<?php
    function advanced_search_query($query) {
        if($query->is_search()) {
            $get_the_category_name = $_GET['category_name'];
            if (isset($get_the_category_name) && is_array($get_the_category_name)) {        
            $catnames = implode(",",$get_the_category_name);
            $query->set('category_name', $catnames);
            return $query;
            }
        }
    }
    add_action('pre_get_posts', 'advanced_search_query');
    ?>

if you do:

var_dump($get_the_category_name);

You'll get:

array(2) { [0]=> string(22) "category-a" [1]=> string(25) "category-b" } 

Run implode() on that

string(48) "human-element-analysis,latent-defects-check-list" 

Stick that string in a variable, set it in query as category_name, win.

Thanks again Preethi for putting me in the right direction, and hope the above helps anyone looking to do the same!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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