[Resolved] Limit custom search results

Home Forums Support Limit custom search results

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • #1421411

    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!

    Lead Developer
    Lead Developer

    Hi there,

    You’d need to do something like this, instead:

    add_shortcode( 'searchform', function() {
            <form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
                    <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" />
                <input type="submit" class="search-submit" value="&#xf002" />
        return ob_get_clean();
    } );

    Then just replace POST_TYPE_SLUG_HERE with the slug of your custom post type.


    Awesome, thanks Tom!

    Lead Developer
    Lead Developer
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.