convert to plugins+theme
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* 归档扩展搜索:在新闻/公告归档搜索时,额外按作者显示名/登录名/昵称匹配。
|
||||
* 由主题模板通过查询变量 itstudio_archive_extend_search / itstudio_archive_keyword 触发。
|
||||
* 抽离自主题 functions.php。
|
||||
*/
|
||||
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
function itstudio_archive_extended_search_enabled($query) {
|
||||
if (!($query instanceof WP_Query)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$query->get('itstudio_archive_extend_search')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$keyword = trim((string) $query->get('itstudio_archive_keyword'));
|
||||
return $keyword !== '';
|
||||
}
|
||||
|
||||
function itstudio_archive_extended_search_join($join, $query) {
|
||||
if (!itstudio_archive_extended_search_enabled($query)) {
|
||||
return $join;
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
if (strpos($join, $wpdb->users) !== false) {
|
||||
return $join;
|
||||
}
|
||||
|
||||
return $join . " LEFT JOIN {$wpdb->users} ON ({$wpdb->posts}.post_author = {$wpdb->users}.ID) ";
|
||||
}
|
||||
add_filter('posts_join', 'itstudio_archive_extended_search_join', 10, 2);
|
||||
|
||||
function itstudio_archive_extended_search_where($where, $query) {
|
||||
if (!itstudio_archive_extended_search_enabled($query)) {
|
||||
return $where;
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
$keyword = trim((string) $query->get('itstudio_archive_keyword'));
|
||||
$like = '%' . $wpdb->esc_like($keyword) . '%';
|
||||
|
||||
$search_sql = $wpdb->prepare(
|
||||
" AND (
|
||||
{$wpdb->posts}.post_title LIKE %s
|
||||
OR {$wpdb->posts}.post_excerpt LIKE %s
|
||||
OR {$wpdb->posts}.post_content LIKE %s
|
||||
OR {$wpdb->users}.display_name LIKE %s
|
||||
OR {$wpdb->users}.user_login LIKE %s
|
||||
OR {$wpdb->users}.user_nicename LIKE %s
|
||||
)",
|
||||
$like,
|
||||
$like,
|
||||
$like,
|
||||
$like,
|
||||
$like,
|
||||
$like
|
||||
);
|
||||
|
||||
return $where . $search_sql;
|
||||
}
|
||||
add_filter('posts_where', 'itstudio_archive_extended_search_where', 10, 2);
|
||||
Reference in New Issue
Block a user