不知道大家有没有遇到过一个烦恼,就是在WordPress搜索自己想找的文章的时候,真的是很费劲,半天也找不到自己要找的那个链接
没有想到还真有人跟我一样的烦恼
如何实现WordPress后台标题搜索
我找到Stackexchange论坛有网友分享这个问题
这里是帖子内容:
I have a site with thousands of posts, and in the admin interface I simply want to be able to find and edit posts quickly based on their title.
我有一个包含数千个帖子的网站,在管理界面中我只是希望能够根据帖子的标题快速查找和编辑帖子。
The default post search functionality matches on the entire content of each post, and I typically get hundreds of results for any given search. I then have to click though many screens to actually locate the post I want to edit, which takes a long time.
默认的帖子搜索功能会匹配每个帖子的全部内容,对于任何给定的搜索,我通常会得到数百个结果。然后我必须点击许多屏幕才能真正找到我想要编辑的帖子,这需要很长时间。
Ideally I would just like to start typing a post title, see an auto-complete list of matching posts, and then click on the one I’m looking for.
理想情况下,我只想开始输入帖子标题,查看匹配帖子的自动完成列表,然后单击我要查找的帖子。
Does anyone know of a setting or plugin to solve this problem?
有谁知道可以解决此问题的设置或插件?
解决方案
WordPress插件:WP Extended Search
链接:https://wordpress.org/plugins/wp-extended-search/
添加代码
我还真找到了
大家可以使用代码来实现,把下面的代码添加到你的functions.php文件即可
中文版
/**
* 让 WordPress 只搜索文章的标题
*/
function __search_by_title_only( $search, &$wp_query )
{
global $wpdb;
if ( empty( $search ) )
return $search; // skip processing - no search term in query
$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', '__search_by_title_only', 500, 2 );
我把上面的代码注释改成英语版的
/**
* WordPress Admin Search by Title
*/
function __search_by_title_only( $search, &$wp_query )
{
global $wpdb;
if ( empty( $search ) )
return $search; // skip processing - no search term in query
$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', '__search_by_title_only', 500, 2 );
我用一样的关键词去搜索,直接就定位到文章标题了
这样就可以快速查找发布的文章,进行修改了。