0

I am using WP_Query to search results but this is not taking multiple words in AND operation.

this is my code

$searched_string = $_POST['search'];
$the_query = new WP_Query( 
        array( 
            'post_type' => 'press-release',
            'posts_per_page' => -1,
            's' => $searched_string 
        ) 
    );

when I searched for apple introduces it fetches all those results whose either title having apple or introduces or content having apple or introduces

while I want all words in a combination.

Please help thanks.

  • This is how WordPress searches for words! You can use this plugin where it provide the way to choose between AND/OR – Sumit Apr 21 '16 at 8:48
2

Wordpress also provide more filter to can change wp query using filters. What i need logical to use filter to override query below sample of code if this helpful for you.

add_filter( 'posts_where', 'posts_where_condition_search' );
 $wp_query = new WP_Query( 
        array( 
            'post_type' => 'press-release',
            'posts_per_page' => -1,
        ) 
    );  
 remove_filter( 'posts_where', 'posts_where_condition_search' );    

 function posts_where_condition_search( $where ){

    $search_string = 'test';

    if( isset( $_POST['search']) && !empty($_POST['search']) )
    $search_string = $_POST['search'];

    if( empty($search_string) )
    return $where;
    // add your multiple condition here and write your custom filter

    $where .= " AND (((wp_posts.post_title LIKE '%$search_string%') OR (wp_posts.post_content LIKE '%$search_string%'))) ";
    //$where .= " AND (((wp_posts.post_title LIKE '%$search_string%') OR (wp_posts.post_content LIKE '%$search_string%'))) ";
    //$where .= " AND (((wp_posts.post_title LIKE '%$search_string%') OR (wp_posts.post_content LIKE '%$search_string%'))) ";

   return $where;
}

https://codex.wordpress.org/Class_Reference/WP_Query#Filters

|improve this answer|||||

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.