Archived topic
Limit custom search results
3 replies · Started by Kim on August 28, 2020
Hi guys,
I followed Tom's instructions from this post to create a custom search shortcode.
I was wondering if I might be able to limit what post types are returned by this search?
I have two custom post types (pay-per-view video and freeview videos) and I have a search bar at the top of the archive for both of these post types. How would I go about restricting the search result to only this post type?
Any advice is much appreciated, as always!
Hi there,
You'd need to do something like this, instead:
add_shortcode( 'searchform', function() {
ob_start();
?>
<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<label>
<span class="screen-reader-text">Search for:</span>
<input type="search" class="search-field" placeholder="Search..." value="<?php echo get_search_query(); ?>" name="s" />
<input type="hidden" name="post_type" value="POST_TYPE_SLUG_HERE" />
</label>
<input type="submit" class="search-submit" value="" />
</form>
<?php
return ob_get_clean();
} );
Then just replace POST_TYPE_SLUG_HERE with the slug of your custom post type.
Awesome, thanks Tom!
You're welcome :)