Dùng Fulltext search kết hợp với tìm kiếm truyền thống phần 3 (ok)
function wpdocs_save_meta_box( $post_id, $post, $update ) {
$categories = [];
$sting = $sting2 = "";
$categorie1 = get_the_terms($post_id, 'area_category') ? get_the_terms($post_id, 'area_category') : [];
$categorie2 = get_the_terms($post_id, 'welfare_category') ? get_the_terms($post_id, 'welfare_category') : [];
$categorie3 = get_the_terms($post_id, 'job_category') ? get_the_terms($post_id, 'job_category') : [];
$categorie4 = get_the_terms($post_id, 'recruitment_category') ? get_the_terms($post_id, 'recruitment_category') : [];
$categories = array_merge($categorie1,$categorie2,$categorie3,$categorie4);
$p3_re = get_post_meta($post_id,'p3_re');
$p6_re_0_p6_title = get_post_meta($post_id,'p6_re_0_p6_title');
$p6_re_0_p6_content = get_post_meta($post_id,'p6_re_0_p6_content');
$p4_t1 = get_post_meta($post_id,'p4_t1');
$p4_t2 = get_post_meta($post_id,'p4_t2');
$p4_t3 = get_post_meta($post_id,'p4_t3');
$categories2 = array_merge($p3_re,$p6_re_0_p6_title,$p6_re_0_p6_content,$p4_t1,$p4_t2,$p4_t3);
if(!empty($categories2)) {
foreach ($categories2 as $key => $categorie2) {
$sting2 .= " " .trim($categorie2). " ";
}
}
$sting2 = trim(preg_replace("/\s\s+/", " ", $sting2));
if(!empty($categories)) {
foreach ($categories as $key => $categorie) {
$sting .= " " .trim($categorie->name). " ";
}
}
$sting = trim($sting);
if($sting2) {
$sting = $sting . " " . $sting2;
}
if( $sting ) :
update_post_meta($post_id, 'set_post_category_taxonomy', $sting);
estar_child_move_data_company($post_id,$sting);
endif;
}
add_action( 'save_post', 'wpdocs_save_meta_box',10,3 );
function estar_child_move_data_company( $post_id ,$sting) {
global $wpdb;
$data = [];
$data['ID'] = $post_id;
$data['set_post_category_taxonomy_custom'] = get_post_meta( $post_id, 'set_post_category_taxonomy', true );
$data = array_filter( $data );
$reset = $wpdb->query( $wpdb->prepare( " SELECT * FROM wp_post_category_taxonomy WHERE ID = $post_id ") );
if ( $data && $reset ) {
$wpdb->query( $wpdb->prepare( " UPDATE wp_post_category_taxonomy SET set_post_category_taxonomy_custom = '$sting' WHERE wp_post_category_taxonomy.ID = $post_id ") );
}else {
$wpdb->insert( 'wp_post_category_taxonomy', $data );
}
}
// ??
$use_custom_search = !empty($_GET['s']);
if ($use_custom_search) {
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' );
add_filter('posts_orderby', 'orderby_pages_callback', 10, 2);
return $search;
}
function posts_where($where) {
global $wpdb;
$keywords = get_query_var('s');
$newout = $keywords; // 「残業 福岡」
$newout = $test = "";
$newout .= preg_replace('/\s+/', ' ', $keywords);
$newout = trim($newout);
$leng = explode(' ', $newout);
$lengs = $lengs2 = array();
foreach($leng as $le) {
$lengs[] ="%{$le}%";
}
foreach($leng as $le) {
$lengs2[] ="{$le}";
}
$newout2 = implode(" ",$lengs2);
foreach ($lengs as $value) {
$test .= " OR wp_post_category_taxonomy.set_post_category_taxonomy_custom LIKE '{$value}' ";
}
$where = " AND {$wpdb->posts}.post_status = 'publish' AND {$wpdb->posts}.post_type = 'post' AND ((MATCH({$wpdb->posts}.post_title) AGAINST ('$newout2' IN BOOLEAN MODE) AND {$wpdb->posts}.post_status = 'publish') OR (MATCH(wpma.meta_value) AGAINST ('{$newout2}' IN BOOLEAN MODE) AND {$wpdb->posts}.post_status = 'publish')) ";
$where .= $test;
return $where;
}
function __custom_join_tables($joins) {
global $wpdb;
$joins .= " LEFT JOIN $wpdb->postmeta AS wpma ON (wpma.post_id = {$wpdb->posts}.ID) LEFT JOIN wp_post_category_taxonomy ON (wp_post_category_taxonomy.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;
}
function orderby_pages_callback($orderby_statement, $wp_query) {
global $wpdb;
# Verify correct post type, or any other query variable
if ($wp_query->get("post_type") == "post") {
# In this trivial example add a reverse menu order sort
return " {$wpdb->posts}.post_date DESC ";
} else {
# Use provided statement instead
return $orderby_statement;
}
}
add_action('posts_search', '__adapted_search_function', 1, 2);
}
Last updated
Was this helpful?