It sounds like you’re encountering several issues with customizing search and tag functionalities in WordPress, particularly concerning portfolio and blog posts. Let’s tackle each point step by step.
- Filtering Search Results to Include Only Posts and Portfolio Posts
Your initial function in functions.php is almost correct, but it seems to be improperly filtering the post types. Here’s a revised version:
add_action( 'pre_get_posts', 'cpt_in_search_results', 1 );
function cpt_in_search_results( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if ( $query->is_search() ) {
$query->set( 'post_type', array( 'post', 'portfolio' ) );
}
}
In this code, we’re adjusting the query only when it’s a main query (not a secondary one) and when it’s a search query. We set the post_type to include only ‘post’ and ‘portfolio’.
-
Ensuring Portfolio Posts Use the Same Tags as Blog Posts
Your approach to adding the same taxonomies (categories and tags) to both posts and portfolio post types seems correct. However, it’s important to ensure that these taxonomies are properly registered for the portfolio post type. Sometimes custom post types don’t behave exactly like standard posts, especially with taxonomies. -
Tag Page Results and Templates
Regarding the issue of tag pages and templates, it sounds like there might be a conflict or misunderstanding in how WordPress handles archive pages for different post types. WordPress uses the archive.php template for displaying a list of posts for a given taxonomy term.
Same Template for Different Post Types: To ensure that both portfolio and blog posts use the same template, you might need to create a custom archive template specifically for tags that includes logic to handle both post types. You can create a tag.php in your theme and customize it as needed.
No Results for Portfolio Tags: This could be an issue with how the portfolio tags are queried. Make sure your query logic in tag.php correctly fetches posts from both post and portfolio types.
- Customizing the Search Page
To customize the search results page, you can create a search.php template in your theme. This template can be structured similarly to archive.php, but you can customize it specifically for search results.
Here’s a basic structure for search.php:
<?php get_header(); ?>
<main role="main">
<?php if ( have_posts() ) : ?>
<header>
<h1><?php printf( __( 'Search Results for: %s', 'your-text-domain' ), get_search_query() ); ?></h1>
</header>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'search' ); // Display search content ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); // No search results ?>
<?php endif; ?>
</main>
<?php get_footer(); ?>
In this template, you can customize how each search result is displayed by editing the content-search.php template part.
Remember to clear any caching (like WP Rocket) and test these changes in a staging environment if possible, to avoid disrupting your live site. If you’re still facing issues, it could be worth exploring whether any plugins or the theme itself might be interfering with the expected behavior.