0

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.

0

Well, in meantime, I found the solution:

I've changed the filter and looks like this now:

add_filter( 'posts_where', 'general_filter', 10, 2 );
$posts = new WP_Query($args);
remove_filter( 'posts_where', 'general_filter', 10, 2 );

and the function...

function general_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 ) ) . '%\' OR ' . $wpdb->posts . ' .post_excerpt LIKE \'%' . esc_sql( like_escape( $search_term ) ) . '%\' OR ' . $wpdb->posts . ' .post_content LIKE \'%' . esc_sql( like_escape( $search_term ) ) . '%\')';
    }
    return $where;
}

The error basically was that the completely query was wrong, so doing debugging the variable $where I found the correct way. Hope this could help someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.