• Resolved kory27

    (@kory27)


    Hi,

    I’m in the research phase and hoping the community can help me save a little time. In short, I have 3 custom post types. I need to create a custom search for each of them, only returning results within each post type on that search results page.

    What is the best strategy to achieve this? I’ve found some info on creating a custom search which I then assume I would call on a custom search template page for each, but this is my first time doing this so any help/insight is appreciated.

    Thanks community!

    Kory

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi,

    You can use input type hidden in your search form to get a specific post type search result :

    <div>   
        <h3>Search Products</h3>
        <form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
        <input type="text" name="s" placeholder="Search Products"/>
        <input type="hidden" name="post_type" value="products" /> <!-- // hidden 'products' value -->
        <input type="submit" alt="Search" value="Search" />
      </form>
     </div>

    You can change the post_type value in hidden input with your own post_type.

    Thread Starter kory27

    (@kory27)

    Thanks so much and sorry for the delay. I hadn’t time to test it out. I’m getting the form to display for my post type names using the code below:

    <div>   
        <h3>Search Products</h3>
        <form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
        <input type="text" name="s" placeholder="Search Names"/>
        <input type="hidden" name="post_type" value="names" /> <!-- // hidden 'names' value -->
        <input type="submit" alt="Search" value="Search" />
      </form>
     </div>

    It’s throwing a 404 on the url http://www.myurl.com//<?s=Mitchell&post_type=names

    Any clue why?

    Thanks again so much!

    Thread Starter kory27

    (@kory27)

    Hi,

    I am using Beaver Builder and had to do this a little differently b/c you can’t put php in a BB html module. I made a shortcut and placed in functions.php. Here is the code:

    
    add_shortcode( 'kory_search', function() {
        ob_start();
        ?>
        <h3>Search Products</h3>
        <form role="search" action="<?php echo site_url( '/' ); ?>" method="get" id="searchform">
            <input type="text" name="s" placeholder="Search Names"/>
            <input type="hidden" name="post_type" value="names" /> <!-- // hidden 'names' value -->
            <input type="submit" alt="Search" value="Search" />
        </form>
        <?php
        return ob_get_clean();
    });
    
    I hope this helps someone and thanks!
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Creating a search to return only specific post types including custom post types’ is closed to new replies.