[Resolved] Limit custom search results

Home Forums Support [Resolved] Limit custom search results

Home Forums Support Limit custom search results

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

    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!

    #1422120
    Tom
    Lead Developer
    Lead Developer

    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="&#xf002" />
            </form>
        <?php
        return ob_get_clean();
    } );

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

    #1422175
    Kim

    Awesome, thanks Tom!

    #1422337
    Tom
    Lead Developer
    Lead Developer

    You’re welcome 🙂

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