Simple way to search custom-post types only

admin2025-01-08  4

I'm just trying to find a way to narrow my current search bar so that it only searches within my 'events' custom post-type.

I do not want the search to index any other post type, only 'events'.

Here's the search bar:

<form id="searchform" action="http://localhost:8888/ltc" method="get">
        <input class="inlineSearch" type="text" name="s" value="Enter a keyword" onblur="if (this.value == '') {this.value = 'Enter a keyword';}" onfocus="if (this.value == 'Enter a keyword') {this.value = '';}" />
        <input class="inlineSubmit" id="searchsubmit" type="submit" alt="Search" value="Search" />
    </form>

And the search.php:

<?php if ( have_posts() ) : ?>
            <h1><?php printf( __( 'Search Results for: %s', 'twentyten' ), '' . get_search_query() . '' ); ?></h1>
            <?php
            /* Run the loop for the search to output the results.
             * If you want to overload this in a child theme then include a file
             * called loop-search.php and that will be used instead.
             */
             get_template_part( 'loop', 'search' );
            ?>

(I haven't actually edited the default search.php page yet, as I just want to get the indexing correct first)

Thanks

I'm just trying to find a way to narrow my current search bar so that it only searches within my 'events' custom post-type.

I do not want the search to index any other post type, only 'events'.

Here's the search bar:

<form id="searchform" action="http://localhost:8888/ltc" method="get">
        <input class="inlineSearch" type="text" name="s" value="Enter a keyword" onblur="if (this.value == '') {this.value = 'Enter a keyword';}" onfocus="if (this.value == 'Enter a keyword') {this.value = '';}" />
        <input class="inlineSubmit" id="searchsubmit" type="submit" alt="Search" value="Search" />
    </form>

And the search.php:

<?php if ( have_posts() ) : ?>
            <h1><?php printf( __( 'Search Results for: %s', 'twentyten' ), '' . get_search_query() . '' ); ?></h1>
            <?php
            /* Run the loop for the search to output the results.
             * If you want to overload this in a child theme then include a file
             * called loop-search.php and that will be used instead.
             */
             get_template_part( 'loop', 'search' );
            ?>

(I haven't actually edited the default search.php page yet, as I just want to get the indexing correct first)

Thanks

Share Improve this question asked Sep 28, 2011 at 17:03 remi90remi90 4172 gold badges10 silver badges25 bronze badges 2
  • To tag along with Hansy check out my post here: wordpress.stackexchange.com/questions/26372/… to use radio checkboxes to give the users an option of what they wish to search. – Brian Fegter Commented Sep 28, 2011 at 18:07
  • for multiple post types: <input type="hidden" name="post_type[]" value="stories"/> <input type="hidden" name="post_type[]" value="products"/> wordpress.stackexchange.com/questions/12723/… – Phil Sav Commented Mar 26, 2018 at 5:45
Add a comment  | 

3 Answers 3

Reset to default 13

To search for a custom post type , you can add to the query &post_type=events , to achieve this just edit your form like this

<form id="searchform" action="http://localhost:8888/ltc" method="get">
        <input class="inlineSearch" type="text" name="s" value="Enter a keyword" onblur="if (this.value == '') {this.value = 'Enter a keyword';}" onfocus="if (this.value == 'Enter a keyword') {this.value = '';}" />
        <input type="hidden" name="post_type" value="events" />
        <input class="inlineSubmit" id="searchsubmit" type="submit" alt="Search" value="Search" />
</form>

You can do this for any post type (needs to be an existent one or will be discarded) and will work just fine

In my case, the search functionality was working perfectly. I just needed to filter out the query to search only for the Learndash Courses post type. Here's what I did:

// Filters the search query to only include 'sfwd-courses' post type
function fn_search_courses_only($query) {
    if( $query->is_search && ! is_admin() ) {
        $query->set( 'post_type', 'sfwd-courses' );
    }
}
add_action( 'pre_get_posts', 'fn_search_courses_only' );

To search for a custom post, you just have to put the post type in the value section.
ex: My Custom post type is "blogpst". Now see what i did actually in the second input field

<input type="hidden" name="post_type" value="blogpst" />

See, What i have did. I just put the post type in value section.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736269297a1341.html

最新回复(0)