Người Viết : ViKiMi Editor

Hướng dẫn tuỳ chỉnh tìm kiếm của WordPress theo những điều kiện khác nhau trên Database!
Custom Search của WordPress mà không cần dùng tới Plugin như một phép thử thú vị và đem lại những kết quả đáng ngạc nhiên. Dựa trên những Custom cơ bản này mà chúng ta có thể đem lại một cách xử lý mới trên Website cho những cách tìm kiếm thông tin riêng. Kết quả có được sẽ có thể thay thể các tìm kiếm cơ bản cung cấp bởi WordPress.
Như một cách an toàn cho các tuỳ chỉnh của bạn, cách lựa chọn tốt nhất có thể thực hiện với Child Theme, hoặc tạo ra các snippet code. Những cách này an toàn tới mức không làm Website của bạn xảy ra các sự cố nghiêm trọng mà không khắc phục nổi. Dưới đây sẽ chỉnh sửa tại Child Theme với file function.php (Nếu chưa có Child Theme thì cũng có thể thực hiện trên Theme hiện tại mang tính thử nghiệm).
Thực hiện truy cập tới trình chỉnh sửa File của Theme để custom Search của WordPress với các cách riêng :
Dashboard, Appearance » Theme Files Editor
A. Thực hiện Custom Search cho phép tìm kiếm theo Tags và Categories
Những tuỳ chỉnh này cho phép bạn thoải mái tìm kiếm Posts theo điều kiện về Tags và Categories. Theo những ý đồ khác nhau mà các tìm kiếm có thể có các điều kiện khác nhau. Những rằng buộc tìm kiếm bên dưới sẽ cho bạn các kết quả tìm kiếm chính xác hơn.
/**
*
* Search Posts by Tags and Categories
*
*/
/**
* Join the terms, term_relationship, and term_taxonomy tables.
*
* @global $wpdb
*
* @param string $join The JOIN clause.
* @param object $query The current WP_Query instance.
*
* @return string The JOIN clause.
*/
function search_custom_posts_join( $join, $query ) {
global $wpdb;
if ( ! is_main_query() || ! is_search() ) {
return $join;
}
$join .= “
LEFT JOIN
(
{$wpdb->term_relationships}
INNER JOIN
{$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
INNER JOIN
{$wpdb->terms} ON {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id
)
ON {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id “;
return $join;
}
add_filter( ‘posts_join’, ‘search_custom_posts_join’, 10, 2 );
/**
* Modify the WHERE clause to include searches against taxonomies.
*
* @global $wpdb
*
* @param string $where The WHERE clause.
* @param WP_Query $query The current WP_Query instance.
*
* @return string The WHERE clause.
*/
function search_custom_posts_where( $where, $query ) {
global $wpdb;
if ( ! is_main_query() || ! is_search() ) {
return $where;
}
// Get additional where clause for the user.
$user_where = ”;
$user_id = get_current_user_id();
$status = array( “‘publish'” );
// Include private posts if the user is logged in.
if ( $user_id ) {
$status[] = “‘private'”;
$user_where .= ” AND {$wpdb->posts}.post_author = {$user_id}”;
}
$user_where .= ” AND {$wpdb->posts}.post_status IN( ” . implode( ‘,’, $status ) . ” ) “;
$where .= ” OR (
{$wpdb->term_taxonomy}.taxonomy IN( ‘category’, ‘post_tag’ )
AND
{$wpdb->terms}.name LIKE ‘%” . esc_sql( get_query_var( ‘s’ ) ) . “%’
{$user_where}
)”;
return $where;
}
add_filter( ‘posts_where’, ‘search_custom_posts_where’, 10, 2 );
/**
* Set the GROUP BY clause to post IDs.
*
* @global $wpdb
*
* @param string $groupby The GROUPBY clause.
* @param WP_Query $query The current WP_Query instance.
*
* @return string The GROUPBY clause.
*/
function search_custom_posts_groupby( $groupby, $query ) {
global $wpdb;
if ( ! is_main_query() || ! is_search() ) {
return $groupby;
}
return “{$wpdb->posts}.ID”;
}
add_filter( ‘posts_groupby’, ‘search_custom_posts_groupby’, 10, 2 );
B. Thực hiện Custom Search cho phép tìm kiếm theo Nội Dung Comment và Tác Giả
Với tuỳ chình tìm kiếm này, cho phép bạn thực hiện các ý đồ tìm kiếm cho WordPress theo nội dung Comment của bài viết và tác giả bài viết. Các tuỳ chỉnh giúp tìm kiếm tốt hơn và có thể bật các khả năng tìm kiếm khác nhau nếu có giao diện.
/*
*
* Search by Comment Content and Author Names
*
*/
/**
* Joins the comments and posts tables.
*
* @global $wpdb
*
* @param string $join The JOIN clause.
* @param object $query The current WP_Query instance.
*
* @return string The JOIN clause.
*/
function search_custom_comments_posts_join( $join, $query ) {
global $wpdb;
if ( ! is_main_query() || ! is_search() ) {
return $join;
}
$join .= ‘ LEFT JOIN ‘ . $wpdb->comments . ‘ ON ‘ . $wpdb->posts . ‘.ID = ‘ . $wpdb->comments . ‘.comment_post_ID ‘;
return $join;
}
add_filter( ‘posts_join’, ‘search_custom_comments_posts_join’, 10, 2 );
/**
* Modify the WHERE clause to include searches against comments.
*
* @global $wpdb
*
* @param string $where The WHERE clause.
* @param WP_Query $query The current WP_Query instance.
*
* @return string The WHERE clause.
*/
function search_custom_comments_posts_where( $where, $query ) {
global $wpdb;
if ( ! is_main_query() || ! is_search() ) {
return $where;
}
$where = preg_replace(
“/\(\s*” . $wpdb->posts . “.post_title\s+LIKE\s*(\'[^\’]+\’)\s*\)/”,
“(” . $wpdb->posts . “.post_title LIKE $1) OR (” . $wpdb->comments . “.comment_author LIKE $1) OR (” . $wpdb->comments . “.comment_content LIKE $1)”,
$where
);
return $where;
}
add_filter( ‘posts_where’, ‘search_custom_comments_posts_where’, 10, 2 );
/**
* Prevent search results duplicates.
*
* @param string $distinct The DISTINCT clause.
* @param WP_Query $query The current WP_Query instance.
*
* @return string The DISTINCT clause.
*/
function search_custom_comments_posts_distinct( $distinct, $query ) {
if ( ! is_main_query() || ! is_search() ) {
return $distinct;
}
return ‘DISTINCT’;
}
add_filter( ‘posts_distinct’, ‘search_custom_comments_posts_distinct’, 10, 2 );
C. Custom Search cho Post Custom Fields
Để thực hiện tuỳ chỉnh tìm kiếm cho Post Custom Fields bạn cần thực hiện các điều kiện Code như bên dưới. Kết quả tìm kiếm có thể áp dụng cho Post Custom Fields một cách chính xác. Thêm Code sau vào Function.php :
/**
*
* Search by Post Custom Fields
*
*/
/**
* Joins the postmeta and posts tables.
*
* @global $wpdb
*
* @param string $join The JOIN clause.
* @param object $query The current WP_Query instance.
*
* @return string The JOIN clause.
*/
function search_custom_meta_posts_join( $join, $query ) {
global $wpdb;
if ( ! is_main_query() || ! is_search() ) {
return $join;
}
$join .=’ LEFT JOIN ‘.$wpdb->postmeta. ‘ ON ‘. $wpdb->posts . ‘.ID = ‘ . $wpdb->postmeta . ‘.post_id ‘;
return $join;
}
add_filter( ‘posts_join’, ‘search_custom_meta_posts_join’, 10, 2 );
/**
* Modify the WHERE clause to include searches against postmeta.
*
* @global $wpdb
*
* @param string $where The WHERE clause.
* @param WP_Query $query The current WP_Query instance.
*
* @return string The WHERE clause.
*/
function search_custom_meta_posts_where( $where ) {
global $wpdb;
if ( ! is_main_query() || ! is_search() ) {
return $where;
}
$where = preg_replace(
“/\(\s*” . $wpdb->posts . “.post_title\s+LIKE\s*(\'[^\’]+\’)\s*\)/”,
“(” . $wpdb->posts . “.post_title LIKE $1) OR (” . $wpdb->postmeta . “.meta_value LIKE $1)”, $where );
return $where;
}
add_filter( ‘posts_where’, ‘search_custom_meta_posts_where’, 10, 2 );
/**
* Prevent search results duplicates.
*
* @param string $distinct The DISTINCT clause.
* @param WP_Query $query The current WP_Query instance.
*
* @return string The DISTINCT clause.
*/
function search_custom_meta_posts_distinct( $distinct, $query ) {
if ( ! is_main_query() || ! is_search() ) {
return $distinct;
}
return ‘DISTINCT’;
}
add_filter( ‘posts_distinct’, ‘search_custom_meta_posts_distinct’, 10, 2 );
Sau khi thêm những Code tuỳ chỉnh bên trên, bạn đã có thể thực hiện Custom Search của WordPress theo cách riêng. Dựa theo các điều kiện cụ thể mà Các điều kiện tuỳ chỉnh của bạn có thể khác nhau. Các mệnh đề tìm kiếm nên thực hiện theo đúng như bạn thiết kế, cuối cùng hãy Save Function.php và thử trải nghiệm Custom Search.
Những tuỳ chỉnh Code khác nhau sẽ cho kết quả hiển thị khác nhau. Bạn có thể thực hiện các tuỳ chỉnh mới bằng cách thêm các Function cần thiết do bạn tự Mã Code vào các Filter phù hợp của WordPress.