/    Sign up×
Bounties /Pin to ProfileBookmark

How to extend the WordPress Search to Taxonomies?

By default, WordPress Search is somewhat limited and seems to pull results based on Title/Content only. If I write a Post and Tag it “JavaScript” but don’t mention that specific keyword in the Title/Content, searching the site for JavaScript won’t show this Post in the search results despite the Tag.

What would be a minimal function to extend WP search to Tags, Categories, and Custom Taxonomies? Ideally, this could be done without the use of a plugin like WP Extended Search.

to post a answer
PHPWordPress

3 Replies

Copy linkTweet thisAlerts:
@joshuaburlesoOct 24.2022 — There are a few ways to go about this in WP. In terms of ease and extensibility, you should be able to use, and go with, the pre_get_posts hook. How you implement custom taxonomies is going to depend on how you created them. If you used a plug-in then sometimes these have some funky naming conventions (or even on rare occasion have a tricky way of finding them), just check the docs for the plugin if that's the case; it's generally not difficult. In this case I'm going to pretend you have a single self-defined custom taxonomy called "stack".

function expand_search($query) {
if (!is_admin() && $query->is_search) {
$query_term = $query->get('s');
if(!$query_term) return;
$query_terms = explode(' ', $query_term); // optional
// don't forget to sanitize, I'm not going to bother here
array_push($query_terms, $query_term);
$query->set(
'tax_query',
array(
'relation' => 'OR', array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $query_terms
),
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $query_terms
),
array(
'taxonomy' => 'stack',
'field' => 'slug',
'terms' => $query_terms
)
)
)
);
}
}
add_action( 'pre_get_posts', 'expand_search' );


As a side-note, you could of course make your life easier and use a function to generate these very repeated query filter definitions. For example:

function tax_query_filter($taxonomy, $terms, $field = 'slug') {
return array(
'taxonomy' => $taxonomy,
'field' => $field,
'terms' => $terms
);
}


Like I said, this isn't the only way to go about it, but it's the strategy I used when I worked with WP and it worked well.
Copy linkTweet thisAlerts:
Sep 12.2022 — add_filter('posts_where', 'custom_posts_where'); function custom_posts_where($where) {
if (is_search()) {
$where = preg_replace(
"/(\w+).post_title LIKE ('%.*?%')/",
"$1.post_title LIKE $2) OR ($1.post_excerpt LIKE $2",
$where);
} return $where;
} inside functions.php Quite a weird way to do it but it’s a workaround if you don’t want to use the WP extended search plugin
Copy linkTweet thisAlerts:
@ESCRCreativeStudiosAug 26.2022 — add_filter('posts_where', 'custom_posts_where');

function custom_posts_where($where) {
if (is_search()) {
$where = preg_replace(
"/(\w+).post_title LIKE ('%.*?%')/",
"$1.post_title LIKE $2) OR ($1.post_excerpt LIKE $2",
$where);
}

return $where;
}

inside functions.php

Quite a weird way to do it but it’s a workaround if you don’t want to use the WP extended search plugin
×

Success!

Help @buildinteractive spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...