我只需要能够将该术语作为结果本身吐出,并提供该术语的链接(并在搜索计数查询中计数)。因此,如果一个搜索匹配了一个分类术语,那么这个分类术语应该排在第一位,然后是其他结果,无论其顺序如何。
WP_Query::$posts
是WP_Post
对象,并且不能包含(或与之混合)术语/WP_Term
对象。
然而,有一种方法可以实现您的目标—使用get_terms()
搜索分类术语并修改WP_Query
\'sposts_per_page
和offset
参数,使匹配的术语看起来像是实际搜索结果(即帖子)的一部分。
请注意,目前,get_terms()
/WP_Term_Query
仅支持单个搜索关键字—i、 e。foo, bar
被视为一个关键字—在里面WP_Query
, 这将是两个关键字(foo
和bar
).
中的代码/步骤functions.php
:添加此项:
function wpse342309_search_terms( $query, $taxonomy ) {
$per_page = absint( $query->get( \'posts_per_page\' ) );
if ( ! $per_page ) {
$per_page = max( 10, get_option( \'posts_per_page\' ) );
}
$paged = max( 1, $query->get( \'paged\' ) );
$offset = absint( ( $paged - 1 ) * $per_page );
$args = [
\'taxonomy\' => $taxonomy,
// \'hide_empty\' => \'0\',
\'search\' => $query->get( \'s\' ),
\'number\' => $per_page,
\'offset\' => $offset,
];
$query->terms = [];
$terms = get_terms( $args );
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
$query->terms = $terms;
}
$args[\'offset\'] = 0; // always 0
$args[\'fields\'] = \'count\';
$query->found_terms = get_terms( $args );
$query->term_count = count( $query->terms );
$query->terms_per_page = $per_page; // for WP_Query::$max_num_pages
$query->is_all_terms = ( (int) $per_page === $query->term_count );
$query->set( \'posts_per_page\', max( 1, $per_page - $query->term_count ) );
$query->set( \'offset\', $query->term_count ? 0 :
max( 0, $offset - $query->found_terms ) );
}
在
filter_search()
, 添加
wpse342309_search_terms( $query, \'resort\' );
此行之后:
$query->set( \'post_type\', array( \'hotel\', \'post\', \'activities\', \'page\' ) );
英寸search.php
, 对于主要WordPress查询:
<?php if ( have_posts() ) :
$total = $wp_query->found_posts + $wp_query->found_terms;
$wp_query->max_num_pages = ceil( $total / $wp_query->terms_per_page );
echo \'<div class="results">\';
// Display terms matching the search query.
if ( ! empty( $wp_query->terms ) ) {
global $term; // for use in the template part (below)
foreach ( $wp_query->terms as $term ) {
get_template_part( \'template-parts/content\', \'search-term\' );
}
}
// Display posts matching the search query.
if ( ! $wp_query->is_all_terms ) {
while ( have_posts() ) {
the_post();
get_template_part( \'template-parts/content\', \'search-post\' );
}
}
echo \'</div><!-- .results -->\';
// Display pagination.
//the_posts_pagination();
echo paginate_links( [
\'total\' => $wp_query->max_num_pages,
\'current\' => max( 1, get_query_var( \'paged\' ) ),
] );
else :
echo \'<h1>No Posts Found</h1>\';
endif; // end have_posts()
?>
英寸search.php
, 对于自定义WordPress查询:
您应该始终手动调用
wpse342309_search_terms()
.
<?php
$my_query = new WP_Query( [
\'s\' => \'foo\',
// ... your args here ...
] );
// Manually apply the terms search.
wpse342309_search_terms( $my_query, \'resort\' );
if ( $my_query->have_posts() ) :
$total = $my_query->found_posts + $my_query->found_terms;
$my_query->max_num_pages = ceil( $total / $my_query->terms_per_page );
echo \'<div class="results">\';
// Display terms matching the search query.
if ( ! empty( $my_query->terms ) ) {
global $term; // for use in the template part (below)
foreach ( $my_query->terms as $term ) {
get_template_part( \'template-parts/content\', \'search-term\' );
}
}
// Display posts matching the search query.
if ( ! $my_query->is_all_terms ) {
while ( $my_query->have_posts() ) {
$my_query->the_post();
get_template_part( \'template-parts/content\', \'search-post\' );
}
}
echo \'</div><!-- .results -->\';
// Display pagination.
echo paginate_links( [
\'total\' => $my_query->max_num_pages,
\'current\' => max( 1, get_query_var( \'paged\' ) ),
] );
else :
echo \'<h1>No Posts Found</h1>\';
endif; // end have_posts()
?>
模板第1部分:template-parts/content-search-term.php
<?php global $term;
$term_link = get_term_link( $term, \'resort\' ); ?>
<div class="result">
<div class="result-inner">
<h2><a href="<?php echo esc_url( $term_link ); ?>"><?php echo esc_html( $term->name ); ?></a></h2>
<p class="blog-more"><a href="<?php echo esc_url( $term_link ); ?>" class="button">View Page</a></p>
</div>
</div>
模板第2部分:template-parts/content-search-post.php
<?php $type = get_post_type(); ?>
<div class="result">
<div class="result-inner">
<h2>
<?php if ( $type == "hotel" ) { ?>
Hotel:
<?php } elseif ( $type == "activities" ) { ?>
Activity:
<?php } elseif ( $type == "post" ) { ?>
Blog Post:
<?php } elseif ( $type == "page" ) { ?>
Page:
<?php } else { ?>
Page:
<?php } ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h2>
<p class="blog-more"><a href="<?php the_permalink(); ?>" class="button">View Page</a></p>
</div>
</div>