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.