4

I have a search function on my blog. If I enter a value, say 'fruits' in the search box and it doesn't match in any of the posts but it is the name of a category in the blog then I want the posts that belong to that category to be displayed.

Is it possible to amend the search functionality so it covers the searching of all categories and possibly tags in the blog too?

Many thanks.

2 Answers 2

1
// Above the loop on your search result template:
if ( is_search() ) // Are we on a search result page?
{
    global $wp_query, $query_string;
    // now modify/filter the query (result)
    query_posts( $query_string . 'cat=1&tag=apples+apples' );
}
2
  • 3
    I believe that the above code will take input from the user and search for it within the apple category. What I mean is that I want the search to cover all category and tag names in the blog. So for example, if type in the search box; fruits, then although I may not have posts containing the text 'fruit' I still want posts that belong in that category to appear in the search, likewise if I type in vegetables (another category) in the search box I want post from the vegetables category to display. Apologies for not making this clear in the question, will edit it now.
    – Mr B
    Commented May 15, 2011 at 20:54
  • Why don't you simply make a drop down for all your categories and/or tags and link there? Or have you got too many cats/tags? Btw: Have you already searched the plugin repo for one that is capable of doing this? And: I edited the title of your Q for more clarity.
    – kaiser
    Commented May 16, 2011 at 1:17
0

Here's a function that will search for any categories (or other taxonomies) that match the given string and then returns all the posts that are included in that category.

function searchTermPosts(String $query) {
    // First get the categories / taxonomies that have a 'name like' the query
    $terms = get_terms([ "name__like" => $query ]);

    // Now convert to a taxonomy query we can use in WP_Query
    $tax_query = array_map(function ($term) {
        return [
            "taxonomy" => $term->taxonomy,
            "terms" => $term->term_taxonomy_id
        ];
    }, $terms);

    // Add an "OR" clause to find posts in all categories
    $tax_query["relation"] = "OR";

    // Now do the query
    $results = new \WP_Query([
        "tax_query" => $tax_query
    ]);

    // Return both results and the terms, as well as term names
    return [
        "results" => $results,
        "terms" => $terms,
        "term_names" => array_map(fn ($t) => $t->name, $terms)
    ];
}

And then use it like this:

$query = searchTermPosts("fruits");
$count = $query['results']->found_posts;
$cats = implode(",", $query['term_names']);

echo "Found $count results in these categories: $cats";
echo "<ul>";

while ($query['results']->have_posts()) {
    $query['results']->the_post();
    echo '<li><a href="' . get_the_permalink() . '">';
    the_title();
    echo '</a></li>';
}

echo "</ul>";

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.