function sps_custom_multiple_keyword_search( $search, $wp_query ) { $search_terms = $wp_query->get( 's' ); $search_terms = trim( $search_terms ); if ( empty( $search_terms ) ) { return $search; } // Check if search query contains the "+" symbol if ( defined('DOING_AJAX') && DOING_AJAX && strpos( $search_terms, '+') !== false ) { $search_terms_array = explode( '+', $search_terms ); $search_terms_array = array_map( 'trim', $search_terms_array ); $search_terms_array = array_filter( $search_terms_array ); if ( empty( $search_terms_array ) ) { return $search; } global $wpdb; $search_sql = array(); foreach ( $search_terms_array as $term ) { $term = esc_sql( $term ); $search_sql[]= "({$wpdb->posts}.post_title LIKE '%{$term}%' OR {$wpdb->posts}.post_content LIKE '%{$term}%')"; } if ( ! empty( $search_sql ) ) { $search_sql = implode( ' OR ', $search_sql ); $search = " AND ({$search_sql})"; } } return $search; } add_filter( 'posts_search', 'sps_custom_multiple_keyword_search', 10, 2 );
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter