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() )
return;
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',
array(
'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.
Thanks.
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() )
return;
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.