Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] An example for how to change the WHERE clause on the search page by using the WordPress API.
<?php
add_filter( 'posts_where', 'acme_search_post_categories' );
/**
* Includes the post meta table in the query that's using the search string.
*
* @param string $where The initial WHERE clause.
* @return string $where The clause for including the post meta table, if on the search template.
*/
function acme_search_post_categories( $where ) {
if ( is_admin() || ! is_search() ) {
return $where;
}
$where =
preg_replace(
"/\(\s*wp_posts.post_title\s+LIKE\s*(\'[^\']+\')\s*\)/" ,
'(wp_posts.post_title LIKE $1) OR
(wp_posts.post_content LIKE $1) OR
(wp_postmeta.meta_value LIKE $1)',
$where
);
return $where;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment