I'm trying to filter one search, and I'm interested in know if a word is inside the title, the content or the excerpt but without luck and finally I'm stucked.
$args = array(
'post_type' => 'post',
'posts_per_page' => '-1',
'search_prod_title' => $_POST['wordToSearch'],
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC'
);
add_filter( 'posts_where', 'title_filter', 10, 2 );
add_filter( 'posts_where', 'content_filter', 10, 2 );
add_filter( 'posts_where', 'excerpt_filter', 10, 2 );
$posts = new WP_Query($args);
remove_filter( 'posts_where', 'title_filter', 10, 2 );
remove_filter( 'posts_where', 'content_filter', 10, 2 );
remove_filter( 'posts_where', 'excerpt_filter', 10, 2 );
And my filter functions are those ones:
function title_filter( $where, &$wp_query )
{
global $wpdb;
if ( $search_term = $wp_query->get( 'search_prod_title' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $search_term ) ) . '%\'';
}
return $where;
}
function content_filter( $where, &$wp_query )
{
global $wpdb;
if ( $search_term = $wp_query->get( 'search_prod_content' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_content LIKE \'%' . esc_sql( like_escape( $search_term ) ) . '%\'';
}
return $where;
}
function excerpt_filter( $where, &$wp_query )
{
global $wpdb;
if ( $search_term = $wp_query->get( 'search_prod_excerpt' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_excerpt LIKE \'%' . esc_sql( like_escape( $search_term ) ) . '%\'';
}
return $where;
}
The point is, the first filter function is working properly, so I can search by title and the results are good, but the results for the rest of the filters are wrong or not working.