7折
减价出售
¥799
在网站运营中难免会使用到WordPress的搜索功能,WordPress主题自带的默认搜索功能,是使用模糊搜索,包括文章内容、文章标题与文章标签,在用WordPress建站后,随着文章逐渐增多,默认的搜索功能并不优秀,这样会使得搜索结果不精确,还大大增加了搜索时长,如果你想让它搜索出来的结果更精确、相关,我们今天就来教给大家只搜索wordpress搜索标题。
function hx_search_by_title_only( $search, &$wp_query )
{
global $wpdb;if ( empty( $search ) )
return $search;
$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';
$search =
$searchand = '';
foreach ( (array) $q['search_terms'] as $term ) {
$term = esc_sql( like_escape( $term ) );
$search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
$searchand = ' AND ';
}
if ( ! empty( $search ) ) {
$search = " AND ({$search}) ";
if ( ! is_user_logged_in() )
$search .= " AND ($wpdb->posts.post_password = '') ";
}
return $search;
}
add_filter( 'posts_search', 'hx_search_by_title_only', 500, 2 );
另外一种方案,同样是放到主题文件夹functions.php中
add_filter( 'posts_search', 'dw_search_by_title', 10, 2 );
function dw_search_by_title( $search, $wp_query ) {
if ( ! empty( $search ) && ! empty( $wp_query->query_vars['search_terms'] ) ) {
global $wpdb;
$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';
$search = array();
foreach ( ( array ) $q['search_terms'] as $term )
$search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term ) . $n );
if ( ! is_user_logged_in() )
$search[] = "$wpdb->posts.post_password = ''";
$search = ' AND ' . implode( ' AND ', $search );
}
return $search;
}
减价出售
减价出售
减价出售
减价出售
电话咨询
133-7205-6573
微信咨询