Created
September 2, 2023 19:55
-
-
Save tripflex/03b00e6474ed3d7617bda52fb0f329be to your computer and use it in GitHub Desktop.
WP Job Manager Keywords Search Disregard Order of Terms (untested)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function smyles_custom_posts_search($search, $wp_query) { | |
global $wpdb; | |
if (empty($search)) { | |
return $search; // skip processing - no search term in query | |
} | |
$q = $wp_query->query_vars; | |
$n = !empty($q['exact']) ? '' : '%'; | |
$search = $searchand = ''; | |
foreach ((array)$q['search_terms'] as $term) { | |
$term = $wpdb->esc_like($term); | |
$like = $n . $term . $n; | |
$search .= $wpdb->prepare("{$searchand}MATCH($wpdb->posts.post_title, $wpdb->posts.post_content) AGAINST(%s IN BOOLEAN MODE)", $like); | |
$searchand = ' AND '; | |
} | |
if (!empty($search)) { | |
$search = " AND ({$search}) "; | |
if (!is_user_logged_in()) { | |
$search .= " AND ($wpdb->posts.post_password = '') "; | |
} | |
} | |
return $search; | |
} | |
add_filter( 'job_manager_get_listings', 'smyles_set_posts_search_filter' ); | |
function smyles_set_posts_search_filter( $args ){ | |
add_filter('posts_search', 'smyles_custom_posts_search', 500, 2); | |
return $args; | |
} | |
add_action( 'after_get_job_listings', 'smyles_unset_posts_search_filter' ); | |
function smyles_unset_posts_search_filter(){ | |
remove_filter('posts_search', 'smyles_custom_posts_search', 500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment