I want to limit the search by keywords to only search for exact word not a phrase or mixed content, so if I search for cheese
it needs to be limited only for word cheese
, and right now it will also return content with the word cheeseburger
.
The part of the search query in my modified template query is right here:
if ( ! function_exists( 'get_job_listings' ) ) :
if ( 'top_rating' === $args['orderby'] ) {
$query_args['meta_query'] = [
'relation' => 'OR',
[
'key' => 'rating',
'compare' => 'EXISTS',
],
[
'key' => 'rating',
'compare' => 'NOT EXISTS',
],
];
$query_args['orderby'] = [
'meta_value_num' => 'DESC',
'comment_count' => 'DESC',
];
}
$job_manager_keyword = sanitize_text_field( $args['search_keywords'] );
if ( ! empty( $job_manager_keyword ) && strlen( $job_manager_keyword ) >= apply_filters( 'job_manager_get_listings_keyword_length_threshold', 2 ) ) {
$search_query = $job_manager_keyword;
$query_args['s'] = $search_query;
// $query_args['s'] = '"' . $search_query . '"';
// $query_args['s'] = '/\b' . preg_quote($search_query, '/') . '\b/';
$query_args['sentence'] = true;
add_filter( 'posts_search', 'get_job_listings_keyword_search' );
}
$query_args = apply_filters( 'job_manager_get_listings', $query_args, $args );
if ( empty( $query_args['meta_query'] ) ) {
unset( $query_args['meta_query'] );
}
if ( empty( $query_args['tax_query'] ) ) {
unset( $query_args['tax_query'] );
}
/** This filter is documented in wp-job-manager.php */
$query_args['lang'] = apply_filters( 'wpjm_lang', null );
// Filter args.
$query_args = apply_filters( 'get_job_listings_query_args', $query_args, $args );
do_action( 'before_get_job_listings', $query_args, $args );
// Cache results.
if ( apply_filters( 'get_job_listings_cache_results', true ) ) {
$to_hash = wp_json_encode( $query_args );
$query_args_hash = 'jm_' . md5( $to_hash . JOB_MANAGER_VERSION ) . WP_Job_Manager_Cache_Helper::get_transient_version( 'get_job_listings' );
$result = false;
$cached_query_results = true;
$cached_query_posts = get_transient( $query_args_hash );
if ( is_string( $cached_query_posts ) ) {
$cached_query_posts = json_decode( $cached_query_posts, false );
if (
$cached_query_posts
&& is_object( $cached_query_posts )
&& isset( $cached_query_posts->max_num_pages )
&& isset( $cached_query_posts->found_posts )
&& isset( $cached_query_posts->posts )
&& is_array( $cached_query_posts->posts )
) {
if ( in_array( $query_args['fields'], [ 'ids', 'id=>parent' ], true ) ) {
// For these special requests, just return the array of results as set.
$posts = $cached_query_posts->posts;
} else {
$posts = array_map( 'get_post', $cached_query_posts->posts );
}
$result = new WP_Query();
$result->parse_query( $query_args );
$result->posts = $posts;
$result->found_posts = intval( $cached_query_posts->found_posts );
$result->max_num_pages = intval( $cached_query_posts->max_num_pages );
$result->post_count = count( $posts );
}
}
if ( false === $result ) {
$result = new WP_Query( $query_args );
$cached_query_results = false;
$cacheable_result = [];
$cacheable_result['posts'] = array_values( $result->posts );
$cacheable_result['found_posts'] = $result->found_posts;
$cacheable_result['max_num_pages'] = $result->max_num_pages;
set_transient( $query_args_hash, wp_json_encode( $cacheable_result ), DAY_IN_SECONDS );
}
if ( $cached_query_results ) {
// random order is cached so shuffle them.
if ( 'rand_featured' === $args['orderby'] ) {
usort( $result->posts, '_wpjm_shuffle_featured_post_results_helper' );
} elseif ( 'rand' === $args['orderby'] ) {
shuffle( $result->posts );
}
}
} else {
$result = new WP_Query( $query_args );
}
do_action( 'after_get_job_listings', $query_args, $args );
//remove_filter( 'posts_search', 'get_job_listings_keyword_search' );
return $result;
}
endif;
I can see that $query_args['s']
is a standard query for search keywords, but some standard query modifications I've tried are like these examples:
And there are other examples too, but none of them are working for me.
How can I modify it, so it can search only for the exact word in content?
I see this question has been posted multiple times, but all of the examples I've tried are not working.
Familiarize yourself with SQL syntax and its much easier to cutomize any query perform by wordpress
You can modify ANY query directly using posts_clauses filter including the search query
by default, wordpress perform the search on Post Title, Post Content and Post Excerot using
LIKE
and%
wrap around the search term, a similar syntax belowIt even break each word into a separate LIKE statement,
But since you want to do an exact match, you can add a space between the search term, then another match if its the first or last, something like below search a string in
post_content
columnOr if you're on > MYSQL 8 that support regex, you can perform string search with boundaries, some like ;
Now to modify the seach query is really simply with code reference below
to avoid manually typing all the
LIKES
andOR's
, we can use these two functions below based on how you want the query,For conditionally building
LIKE
andOR
statement on different fields specified to searchFor conditionally building
RLIKE
andOR
statement on different fields specified to searchAlso, print the
$wp_query->request
on the front-end search page, you'll see the SQL perform on the current page, this will help you debug any issue e.i.If you also want to create your own search page, you can simply apply the same query using
$wpdb