Skip Navigation

[Resolved] Search results from only one cpt in archive

This support ticket is created 6 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 10 replies, has 2 voices.

Last updated by Shane 6 years ago.

Assisted by: Shane.

Author
Posts
#1133409

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?

<div class="archive-search"><form method="get" id="searchform" action="<?php echo home_url(); ?>/">
<input type="search" class="sb-search-input" placeholder="<?php _e('Search Projects...', 'wpzoom') ?>" name="s" id="s" autocomplete="off" />
<input name="post_type" type="hidden" value="building" />
<?php
// output all of our Categories
// for more information see http://codex.wordpress.org/Function_Reference/wp_dropdown_categories
$swp_cat_dropdown_args = array(
'show_option_all' => __( 'Select Category' ),
'orderby' => 'title',
'order' => 'ASC',
);
wp_dropdown_categories( $swp_cat_dropdown_args );
?> <?php
echo "<br>";
?>
<input type="submit" id="searchsubmit" class="sb-search-submit" value="<?php _e('Search', 'wpzoom') ?>" />
<span class="sb-icon-search"></span>
</form></div>

#1133764

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Maria,

Thank you for contacting our support.

So essentially you want your search to be locked to only one CPT correct?

Please let me know.

Thanks,
Shane

#1134075

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.

#1134314

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Maria,

Could you send me a link to the page where I can test out the form ?

I might be able to write a custom hook to resolve this for you.
Thanks,
Shane

#1134387

Ok here is the link > hidden link

#1134554

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Maria,

I'm still unclear on something.

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.

Please clarify as soon as possible.

Thanks,
Shane

#1134883

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.

#1135077

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Maria,

Try the following code.

function set_archive_cpt( $query ) {
    if ( $query->is_archive() && $query->is_main_query() && isset($_GET['cat']) ) {
        $query->set( 'post_type', 'my_cpt_slug' );
    }
}
add_action( 'pre_get_posts', 'set_archive_cpt' );

Add it to your functions.php file

Replace my_cpt_slug with the slug of the CPT.

Please try this and let me know if it works.

Thanks,
Shane

#1135111

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' );

#1135146

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Maria,

Would you mind allowing me to have admin access to the site to do some basic debugging to get this working for you ?

Thanks,
Shane

#1136235

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Maria,

Is there a reason why you want the general search to be limited to just the the Building CPT?

If we do this then all the searches will only be of this CPT.

So customers wouldn't be able to do a general search of the site.

Please let me know.

Thanks,
Shane