I have created with toolset a custom post type named 'building'. Inside the archive of this post type named archive-building I put a search form with an input search and a category dropdown with the following code. The problem is that when I just put a keyword to the search input without selecting a category, I get results only from this post type (this is what I want). But If I select a category I get results from all post types I have created. How do I resolve this in order to get results only from this post type when i select a category?
Yes, but not generally my search. The search box is selecting from only this custom post type. I want the dropdown list of category selection to only select from this custom post type. Cause If I choose a category term now as a search filter I am geting results from all cpts.
So you want to restrict the results to only 1 cpt When the dropdown is selected and when the search is filled out ? Currently you say that when you select a category from the dropdown it doesn't work right.
Yes that is correct. The search filled is working ok by itself. If I just put a phrase without selecting a category the results are from one cpt only. The dropdown is not currently selecting from one cpt. It displays all posts on the selected category from all cpts.
Hi. I m afraid it's not working. Still selecting from all cpts.
That is the code i used:
function set_archive_building( $query ) {
if ( $query->is_archive() && $query->is_main_query() && isset($_GET['cat']) ) {
$query->set( 'post_type', 'building' );
}
}
add_action( 'pre_get_posts', 'set_archive_building' );