The Term Archive Priority extension is deprecated
Please use a Taxonomy source instead.
The Term Archive Priority Extension will force taxonomy term archive pages to bubble to the top of search results when a match is found. This allows you to search by taxonomy term and have applicable taxonomy archive pages show up within your search results!
This Extension only applies to supplemental search engines.
Out of the box, all supplemental search engines will apply this change when the plugin is active, but you can filter that down to specific supplemental search engines by doing something like the following:
<?php | |
function my_maybe_include_term_archive( $include, $engine, $terms ) { | |
// only have term archives included for supplemental search engine with name 'supplemental' | |
return ( $engine == 'supplemental' ) ? true : false; | |
} | |
add_filter( 'searchwp_term_archive_enabled', 'my_maybe_include_term_archive', 10, 3 ); |
NOTE: by default, Term Archive Priority supports exact taxonomy term matches. If you would like to enable matching of any word within a taxonomy term, use the following filter:
<?php | |
add_filter( 'searchwp_tax_term_or_logic', '__return_true' ); |
Once activated, you will need to customize your search results loop so as to account for term archive pages in the results. This is a full theme template example outlining how to properly implement the Term Archive Priority Extension:
Note: The snippet below is for SearchWP 4, if you are using SearchWP 3 view the legacy template.
<?php | |
/* Template Name: SearchWP Term Archive Priority Sample */ | |
global $post; | |
// Retrieve applicable query parameters. | |
$search_query = isset( $_GET['searchwp'] ) ? sanitize_text_field( $_GET['searchwp'] ) : null; | |
$search_page = isset( $_GET['swppg'] ) ? absint( $_GET['swppg'] ) : 1; | |
// Perform the search. | |
$search_results = []; | |
$search_pagination = ''; | |
if ( ! empty( $search_query ) && class_exists( '\\SearchWP\\Query' ) ) { | |
$searchwp_query = new \SearchWP\Query( $search_query, [ | |
'engine' => 'supplemental', // The Engine name. | |
'fields' => 'all', // Load proper native objects of each result. | |
'page' => $search_page, | |
] ); | |
$search_results = $searchwp_query->get_results(); | |
$search_pagination = paginate_links( array( | |
'format' => '?swppg=%#%', | |
'current' => $search_page, | |
'total' => $searchwp_query->max_num_pages, | |
) ); | |
} | |
get_header(); ?> | |
<div id="primary" class="content-area"> | |
<main id="main" class="site-main" role="main"> | |
<header class="page-header"> | |
<h1 class="page-title"> | |
<?php if ( ! empty( $search_query ) ) : ?> | |
<?php printf( __( 'Search Results for: %s' ), esc_html( $search_query ) ); ?> | |
<?php else : ?> | |
SearchWP Supplemental Search | |
<?php endif; ?> | |
</h1> | |
<!-- BEGIN Supplemental Engine Search form --> | |
<form role="search" method="get" class="search-form" | |
action="<?php echo site_url( 'search-results/' ); ?>"> | |
<label> | |
<span class="screen-reader-text"> | |
<?php echo _x( 'Search for:', 'label' ) ?> | |
</span> | |
<input type="search" class="search-field" | |
name="searchwp" | |
placeholder="<?php echo esc_attr_x( 'Search...', 'placeholder' ) ?>" | |
value="<?php echo isset( $_GET['searchwp'] ) ? esc_attr( $_GET['searchwp'] ) : '' ?>" | |
title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" /> | |
</label> | |
<input type="submit" class="search-submit" | |
value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" /> | |
</form> | |
<!-- END Supplemental Engine Search form --> | |
</header> | |
<?php if ( ! empty( $search_query ) && ! empty( $search_results ) ) : ?> | |
<?php foreach ( $search_results as $search_result ) : ?> | |
<article class="page hentry search-result"> | |
<?php | |
switch( get_class( $search_result ) ) { | |
case 'WP_Post': | |
$post = $search_result; | |
?> | |
<header class="entry-header"><h2 class="entry-title"> | |
<a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a> | |
</h2></header> | |
<div class="entry-summary"><?php the_excerpt(); ?></div> | |
<?php | |
wp_reset_postdata(); | |
break; | |
case 'SearchWPTermResult': | |
?> | |
<header class="entry-header"><h2 class="entry-title"> | |
<a href="<?php echo $search_result->link; ?>"> | |
<?php echo $search_result->taxonomy; ?>: <?php echo $search_result->name; ?> | |
</a> | |
</h2></header> | |
<div class="entry-summary"> | |
<p><?php echo $search_result->description; ?></p> | |
</div> | |
<?php | |
break; | |
} | |
?> | |
</article> | |
<?php endforeach; ?> | |
<?php if ( $searchwp_query->max_num_pages > 1 ) : ?> | |
<div class="navigation pagination" role="navigation"> | |
<h2 class="screen-reader-text">Results navigation</h2> | |
<div class="nav-links"><?php echo wp_kses_post( $search_pagination ); ?></div> | |
</div> | |
<?php endif; ?> | |
<?php elseif ( ! empty( $search_query ) ) : ?> | |
<p>No results found, please search again.</p> | |
<?php endif; ?> | |
</main> <!-- .site-main --> | |
</div> <!-- .content-area --> | |
<?php get_footer(); ?> |
Changelog
1.2.2
- [Fix] Results handling in some cases
- [Fix] Unwanted application when performing Admin searches in some cases
- [Update] Updated updater
1.2.1
- [Fix] PHP Warning
- [Update] Updated updater
1.2.0
- [New] Support for SearchWP 4
1.1.8
- [New] Support for Live Search
- [Update] Updated updater
1.1.7
- [Fix] Number of arguments passed to
searchwp_term_archive_term_args
- [Fix] Fatal Error when SearchWP is not active
- [Fix] Check for SearchWP core partial matching setting
1.1.6
- [Fix] Fixes an issue with shared taxonomies not appearing in results
1.1.5
- [Fix] Fixes an issue with Live Search integration being too assumptive, it is now opt-in
- [New] New filter
searchwp_term_archive_live_search
to opt in to Live Search integration
1.1.4
- [New] New filter
searchwp_term_archive_term_args
- [Improvement] Better support for Live Ajax Search searches
- [Update] Updated updater
1.1.3
- [Change] Empty terms are no longer ignored by default when using
OR
/%LIKE%
matching - [New] New filter
searchwp_term_archive_term_args
to customizeOR
/%LIKE%
arguments
1.1.2
- [Fix] Fixed an issue with improper results containing terms spanning multiple post types
- [Update] Updated updater
1.1.1
- [Improvement] Better integration with LIKE Terms/Fuzzy Matches for martial term matching
1.1
- [Improvement] Automatic updates based on your SearchWP license key
0.6
- [Improvement] Formatting
0.5
- [Fix] Better offset handling
- [Fix] PHP Warning cleanup
- [Improvement] Results now ordered by the weight of their taxonomy
0.4
- [New] New filter
searchwp_tax_term_or_logic
that allows OR logic in multi-word terms
0.3
- [Fix] Fixed an issue where disabled post types were not taken into account
0.2
- [Fix] Fixed an issue where taxonomies were being set before custom taxonomies were registered
- [Improvement] Consider multi-word terms
0.1
- Initial release