FullText cho cột post_tile, post_excertp, post_content và meta_value (oke)

function __adapted_search_function($search, $query) {
  if (is_admin() || !$query->is_main_query() || !$query->is_search) {
    return;
  }
  global $wpdb;
  $search_term = $query->get('s');
  $search      = " ";
  add_filter("posts_join", '__custom_join_tables');
  add_filter('posts_where', 'posts_where');
  add_filter('posts_fields', 'custom_posts_fields');
  add_filter( 'posts_groupby', 'my_posts_groupby' );
  return $search;
}
function posts_where($where) {
  global $wpdb;
  $keywords = get_query_var('s');
  $newout = $keywords; 
  $newout = "+";
  $newout .= str_replace(" ", "+", $keywords);
  $where = " AND {$wpdb->posts}.post_status = 'publish' AND (MATCH({$wpdb->posts}.post_title) AGAINST ('$newout' IN BOOLEAN MODE) OR MATCH({$wpdb->posts}.post_excerpt) AGAINST ('$newout' IN BOOLEAN MODE) OR MATCH({$wpdb->posts}.post_content) AGAINST ('$newout' IN BOOLEAN MODE) OR MATCH(wpma.meta_value) AGAINST ('$newout' IN BOOLEAN MODE)) ";
  return $where;
}
function __custom_join_tables($joins) {
  global $wpdb;
  $joins .= " JOIN $wpdb->postmeta AS wpma ON (wpma.post_ID = {$wpdb->posts}.ID) ";
  return $joins;
}
function custom_posts_fields($fields) {
  global $wpdb;
  $fields = " * ";
  return $fields;
}
function my_posts_groupby($groupby) {
  global $wpdb;
  $groupby = " {$wpdb->posts}.ID ";
  return $groupby;
}
add_action('posts_search', '__adapted_search_function', 1, 2);

Last updated