Cải thiện khả năng tìm kiếm của WordPress

Cập nhật lần cuối vào

Bạn đang tìm cách cải thiện khả năng tìm kiếm của WordPress? Bài viết này sẽ giúp ích được cho bạn. Trong đoạn code bên dưới mình gợi ý cho các bạn chỉnh sửa lại query tìm kiếm.

function hocwp_ext_improve_search_build_term_query( $search, $chunk_size, $column_name = 'post_title' ) {
	$chunks = HT()->string_chunk( $search, 2 );

	$s = array_shift( $chunks );

	$search = "$column_name LIKE '%$s%'";

	foreach ( $chunks as $value ) {
		$search .= " OR $column_name LIKE '%$value%'";
	}

	$search = trim( $search );

	if ( 0 < count( $chunks ) ) {
		$search = '(' . $search . ')';
	}

	unset( $chunks, $s, $value );

	return $search;
}

function hocwp_ext_improve_search_query_post_ids( $search, $sql, $ppp, $chunk_size, $column_name = 'post_title' ) {
	global $wpdb;

	$search = hocwp_ext_improve_search_build_term_query( $search, $chunk_size, $column_name );

	$sql .= $search;

	$sql .= " LIMIT 0, " . $ppp;

	return $wpdb->get_col( $sql );
}

function hocwp_ext_improve_search_pre_get_posts_action( $query ) {
	if ( $query instanceof WP_Query && $query->is_main_query() ) {
		if ( $query->is_search() || ( is_admin() && isset( $_GET['s'] ) ) ) {
			$search = isset( $query->query_vars['s'] ) ? $query->query_vars['s'] : '';

			if ( ! empty( $search ) ) {
				global $wpdb;

				$ppp = isset( $query->query_vars['posts_per_page'] ) ? $query->query_vars['posts_per_page'] : HT_Util()->get_posts_per_page();

				$sql = "SELECT ID FROM";
				$sql .= " `$wpdb->posts` WHERE post_status = 'publish' AND ";

				$post_type  = isset( $query->query_vars['post_type'] ) ? $query->query_vars['post_type'] : 'post';
				$post_types = (array) $post_type;

				$post_type = array_shift( $post_types );

				$type = "post_type = '$post_type'";

				foreach ( $post_types as $post_type ) {
					$type .= " OR post_type = '$post_type'";
				}

				$type = trim( $type );

				if ( 0 < count( $post_types ) ) {
					$type = '(' . $type . ')';
				}

				$post_type = $type;

				$sql .= $post_type;
				$sql .= ' AND ';

				$save = $sql;

				$slug = sanitize_title_for_query( $search );
				$sql .= "post_name LIKE '%$slug%'";
				$post_ids = $wpdb->get_col( $sql );

				remove_action( 'pre_get_posts', 'hocwp_ext_improve_search_pre_get_posts_action', 99 );

				$args = $query->query_vars;

				$args['fields'] = 'ids';

				$tmp = new WP_Query( $args );
				$tmp->get_posts();

				if ( ! $tmp->have_posts() && ! HT()->array_has_value( $post_ids ) ) {
					$parts = explode( ' ', $search );

					if ( 1 < count( $parts ) ) {
						$post_ids = hocwp_ext_improve_search_query_post_ids( $search, $save, $ppp, 2 );

						if ( ! HT()->array_has_value( $post_ids ) ) {
							$post_ids = hocwp_ext_improve_search_query_post_ids( $search, $save, $ppp, 1 );

							if ( ! HT()->array_has_value( $post_ids ) ) {
								$post_ids = hocwp_ext_improve_search_query_post_ids( $search, $save, $ppp, 2, 'post_content' );
							}
						}
					}

					unset( $parts );
				} else {
					$sql = $save;
					$sql .= "post_name = '$slug'";
					$post_id = $wpdb->get_var( $sql );

					if ( HT()->is_positive_number( $post_id ) ) {
						$post_ids = array( $post_id );
					} else {
						if ( HT()->array_has_value( $post_ids ) ) {
							$post_ids += $tmp->posts;
							$query->set( 'orderby', 'post__in' );
						}
					}
				}

				if ( HT()->array_has_value( $post_ids ) ) {
					$query->set( 'post__in', $post_ids );
					$query->set( 's', '' );
				}

				unset( $ppp, $post_ids, $sql, $tmp, $post_type, $post_types, $type, $save, $slug );
			}

			unset( $search );
		}
	}
}

add_action( 'pre_get_posts', 'hocwp_ext_improve_search_pre_get_posts_action', 99 );

function hocwp_ext_improve_search_get_search_query_filter( $term ) {
	if ( empty( $term ) ) {
		$term = HT()->get_method_value( 's', 'request' );
	}

	return $term;
}

add_filter( 'get_search_query', 'hocwp_ext_improve_search_get_search_query_filter', 99 );

Nhiệm vụ của đoạn code này là tìm cho ra bài viết theo từ khóa một cách chính xác nhất có thể. Ý tưởng là bạn tách từ khóa tìm kiếm ra thành từng nhóm. Tìm chính xác theo post_name trước. Nếu không có bài viết thì tách nhỏ ra tìm theo tiêu đề. Nếu vẫn không có bài viết thì tìm trong nội dung.

Chú ý: Bạn cần phải chỉnh sửa lại thì mới có thể sử dụng được. Bên trong nội dung hàm mình có dùng những hàm tự viết. Chúc bạn thành công.

Theo dõi
Thông báo của
guest

6 Comments
Cũ nhất
Mới nhất Được bỏ phiếu nhiều nhất
Phản hồi nội tuyến
Xem tất cả bình luận
Tịnh Nguyễn Blog
6 năm trước

Em đang online điện thoại, để tối em nghịch thử.

Cái wordpress nó dở ở công cụ tìm kiếm bác ạ, em thì rất hay xài công cụ tìm kiếm nhưng nó tù tù sao ấy.

La Dịu
5 năm trước

Trong đoạn code có mấy hàm mình tự viết nhé, bạn dựa vào nội dung mà thay đổi lại cho phù hợp chứ copy y nguyên thì không chạy được.

Tịnh Nguyễn Blog
5 năm trước

Ủa bài này của tác giả khác nữa à bác Cường ? À đù, dựa vào nội dung mà sửa lại thì hơi bị căng đó nhỉ 😀

Tịnh Nguyễn Blog
5 năm trước

A đù, e quên nhấn hay sao mà nó chuyển qua cái comment mới luôn rầu :v | Blog bác nhiều thành viên không ? Hocban.vn nay được thêm bác Đạt Nguyễn, ngày xưa có bác Phan Đạt mà bác đó chắc bận quá nên chưa viết được bài nào.