I have been trying to filter my search result without success. Here is my function in functions.php I want to retrieve only post and portfolio posts (no pages). Below is the code I found on your community forum. It sounds like it doesn't work for me.

add_action( 'pre_get_posts', 'cpt_in_search_results', 1 );
    function cpt_in_search_results( $query ){
         if ( is_admin() || ! $query->is_main_query() )
if ( $query->is_search() ) {
        $query->set( 'post_type', array( 'post', 'portfolio' ) );
        $query->set( 'category', array( 'blog', 'portfolio' ) );
        $query->set( 'post__not_in', array('page') );

Previously, I added the tag post to portfolio

function adding_post_tag_portfolio() {
register_post_type( 'portfolio',
            'taxonomies' => array('category', 'post_tag') // this is IMPORTANT

add_action( 'init', 'adding_post_tag_portfolio' );

And the tagportfolio into post

function adding_portfolio_tag_in_blog( ) { 
   register_taxonomy_for_object_type('tagportfolio', 'post');
add_action( 'init', 'adding_portfolio_tag_in_blog',200 );

In order to use the same tags whether it's a portfolio or a post. I got a tag page result when it's a post tag (category Blog). But no result when it's a portfolio tag (category Portfolio).

The last point I cannot deal with: I would like to have the same template retrieving the result. When I click on the tag cloud (if the tag is from blog), the template is good through archive.php. But, if it's a tag from the portfolio, it goes to an other template (with no result). However, it's still archive.php. And the url for the both is: url/tag/word.

I get lost.

And in bonus, my last-last question. I look for customizing the search page as the archive good one... Didn't find the way.

Help will be grantly appreciated.


EDIT1: After browsing the Web, It seems that the pre_get_posts hooks would help me to filter at the same time the post_tag from post and from portfolio. Any idea how to write this out? For instance, may I mix 2 queries as is:

add_action( 'pre_get_posts', 'get_posts_plus_cpt' );
function get_posts_plus_cpt( $query ) {
  if ( is_admin() || ! $query->is_main_query() )
    if ( $query->is_search() ) {
        $taxquery =  array ( 
            'taxonomy' => array ('tagportfolio', 'post_tag') 
        $query->set('tax_query', $taxquery);
      $query->set( 'post_type', array ('post', 'portfolio' ));

With this, I face the displaying of pages that I was able to avoid with the previous query...Any idea? Is pre_get_posts the approriate approach?

EDIT2: I still looking for the good function to retrieve only portfolio and posts from the search or tag cloud. I've tried this also without success.

function tg_include_custom_post_types( $query ) {
    if ( $query->is_main_query() && ! is_admin() && ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) ) {
        $query->set( 'post_type', array( 'post', 'portfolio' ) );
add_action( 'pre_get_posts', 'tg_include_custom_post_types',99 );

But no way. Unable to get a solution on it. When I click on a tag which is from a portfolio cpt, I got the no result.php page.

EDIT3: I am sorry for all these edits. But deadline... and it's almost there but not yet... Here is the search filter in my functions.php via pre_get_posts I use so far:

function search_filter($query) {
    if ( ! is_admin() && $query->is_main_query() ) {
        if ( ($query->is_tag() ) || ( $query-> is_search() ) ) {
            $query->set( 'post_type', 'post', 'portfolio' );
add_action( 'pre_get_posts', 'search_filter' );

It seems to work: I got portfolios and posts dislayed with search input and tags from tagcloud. I don't know if it's thanks to this function because I do so many tests but the portfolio posts and the post from posts come out. However, when I click on a tag in the tag cloud, posts come out unrelated to the keyword.

Is there a way to ask only posts with the tag I click on? And is it from this function I should ask? Or should I put some queries in my tag.php?

Then, when I click on a tag from portfolio, a page still comes out despite my search_filter. Is there a way to exclude especially this page by name and where should I say it?

Thank you for your help.

2 Answers 2


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.

  1. 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() )

    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'.

  1. 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.

  2. 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.

  1. 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() ) : ?>
            <h1><?php printf( __( 'Search Results for: %s', 'your-text-domain' ), get_search_query() ); ?></h1>

        <?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; ?>

<?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.

  • Thx for your comments. 1) Your cpt_in_search_results( $query ) still doesn't work. 2) Regarding the tags whether it's post or portfolio. I've checked the link to taxonomies in the dashboard to see what's going on: for portfolio, taxonomy=post_tag&tag_ID=22&post_type=portfolio and for post: taxonomy=post_tag&tag_ID=22&post_type=post. Is there a way to force the system to show the tag whether it comes from portfolio or post? via htaccess for instance? or via a hook? 3) It sounds WP theme displays different template if the tag comes from post or tag comes from portfolio. I'll figure that out.
    – made leod
    Jan 29 at 14:45

I put my answer to help others who can face the same issue. Thank you to all of the people who help me out to find the solution and valuable comments from this post.

Please see my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.