Using Elasticsearch for your WordPress site search is a powerful feature. It allows for advanced faceting options and generating highly relevant search results. It’s possible to increase the weight of keywords in specific fields like titles and bylines, add a date decay algorithm, and much more. There are several powerful plugins like SearchPress and ElasticPress that make setting up this integration a breeze. However, a common limitation out-of-the-box is that archive pages don’t return in the search results.
It’s common that when a person searches for the name of a Custom Post Type Archive (like “Staff Directory”), or the name of a Category or Post Tag, that you’d want to return the archive page first, as this is most relevant to the user’s search query.
One way to implement this is to edit the search template page in your site’s WordPress theme. The template for the Search Results Page is search.php If you are new to the concept of how WordPress templates work, review this great documentation on The Template File Hierarchy. If your theme doesn’t already have a search.php file, then the search page is using index.php. You’ll want to create a search.php file so that you can customize the search results page without changing any other views.
How to Include CPT Archive Pages and Term Pages in the Search Results
Rather than trying to get archive or term pages to return in the search results, you can add PHP code that compares the search query to the term name and or the archive name.
Example Code for Custom Search Template
Here’s an example of what this could look like. You’ll need to refine it for your theme. This code could also be adjusted so that it’s not only checking for an exact match. Perhaps if the search result contains “event” then you’d want to display the Event archive.
The keywords for each archive page could even be configurable on a settings page in WordPress. Additionally, you could use a similar method to display external links! The options are endless!
<?php
// Get the current search query
$search_query = get_query_var( 's' );
get_header();
?>
<main>
<h1>Search Results for: <?php echo esc_html( $search_query ); ?></h1>
<ul class="search-results">
<?php
// 1. Check for exact tag match
$matched_tag = get_term_by( 'name', $search_query, 'post_tag' );
if ( $matched_tag && ! is_wp_error( $matched_tag ) ) :
$tag_link = get_tag_link( $matched_tag->term_id );
?>
<li class="manual-result tag-result">
<h2><a href="<?php echo esc_url( $tag_link ); ?>"><?php echo esc_html( $matched_tag->name ); ?> (Tag Archive)</a></h2>
<p>View all posts tagged "<?php echo esc_html( $matched_tag->name ); ?>".</p>
</li>
<?php
// 2. Check for CPT archive matches
$custom_archives = [
'person' => 'Staff Directory',
// Add more CPTs as needed: 'event' => 'Event Listings'
];
foreach ( $custom_archives as $post_type => $label ) {
if ( stripos( $search_query, strtolower( $label ) ) !== false ) {
$archive_link = get_post_type_archive_link( $post_type );
if ( $archive_link ) :
?>
<li class="manual-result archive-result">
<h2><a href="<?php echo esc_url( $archive_link ); ?>"><?php echo esc_html( $label ); ?></a></h2>
<p>Visit the <?php echo esc_html( $label ); ?> archive page.</p>
</li>
<?php
endif;
}
}
?>
<?php
// Display the Regular Search Results
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<li>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div><?php the_excerpt(); ?></div>
</li>
<?php endwhile; ?>
<?php else : ?>
<li>No results found.</li>
<?php endif; ?>
</ul>
<?php the_posts_pagination(); ?>
</main>
<?php get_footer(); ?>
Is this how you would solve this? Or do you have a better idea?
Please share your thoughts in the comments below!