-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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