我在functions.php中添加了一个功能,以改善我的目录部分中的搜索功能:http://www.highwaysindustry.com/?post_type=directory_listings&s=king
但是,启用以下功能后,它还会覆盖对网站其余部分的搜索:http://www.highwaysindustry.com
这个特殊的功能我希望它只是目标搜索的目标。
functions.php中的函数
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 REGEXP '[[:<:]]{$term}[[:>:]]')";
$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', 1000, 2 );
我的目录-search.php的片段,目录使用的表格:
<?php
$args = array(
'post_type'=> 'directory_listings',
's' => $s,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'membership_type',
'orderby' => 'meta_value',
'order' => "DESC"
,
),
),
);
?>
<div class="row">
<div class="small-12 columns">
<div class="directory-banner">
<img src="<?php echo site_url(); ?>/wp-content/themes/highways/images/directory-banner.jpg" alt="" title="" />
<h1><?php echo $s; ?></h1>
</div>
</div>
</div>
**我目前已经将上面定义的功能注释掉了,当启用它时更具体,例如,搜索“King”只会提供2个结果。现在,通用搜索提供了太多非特定结果。
答案 0 :(得分:1)
The other 2 answers are also worth investigating. However I'll stick to your solution.
To search only directory_listings post type, tweak the function as shown below
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;
// post_type query var has other posts types as well, return default search
if( ! 'directory_listings' == $q['post_type'] )
return $search
// We are here, it means post_type var contains only directory_listings
// continue with the rest of the code
........
Answering your 2nd query
There is a company called blakedale, if you search just blake you won't get the result. Any method to increase the method of the returned result(s).
The regexp being used in the code below searches for full words hence "blake" doesn't return any results
$search .= "{$searchand}($wpdb->posts.post_title REGEXP '[[:<:]]{$term}[[:>:]]')";
You need to either change the regexp or use SQL LIKE
$search .= "{$searchand}($wpdb->posts.post_title LIKE '%{$term}%')";
P.S: I haven't tested the line of code above, so it may need polishing.
答案 1 :(得分:0)
您是否尝试使用pre_get_posts
?
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search && $_GET['post_type'] == 'directory_listings') {
// add custom meta / tax queries
}
}
}
add_action('pre_get_posts','search_filter');
答案 2 :(得分:0)
你可以试试这个。
$query_args = array( 'post_type' => 'directory_listings', 'tax_query' => array() );
if ( !empty( $search_terms ) ) {
$query_args['s'] = $search_terms;
}
if ( !empty( $search_terms ) ) {
$query_args['tax_query'][] = array(
array(
'taxonomy' => 'directory_listings',
'field' => 'slug',
'terms' => $news_category
)
);
}
$my_posts = new WP_Query( $query_args );
if ( $my_posts->have_posts() ) { // etc
}