Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created November 6, 2015 12:44
Show Gist options
  • Save tommcfarlin/17657f524995b42db455 to your computer and use it in GitHub Desktop.
Save tommcfarlin/17657f524995b42db455 to your computer and use it in GitHub Desktop.
[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