1

I have a site which has 6 custom post types, each one has a search function to only search in that particular post type.

The search works as expected and displays the search results. Each custom post type also has some custom alphabetical pagination which also shows on search results pages, this viewing the custom post type archive, but when you then search from that archive, the search URL reverts back to root. For example :

mydomain.com/?s=Walsall&post_type=clubs

But to get my pagination working the search needs to retain its CPT slug like this :

mydomain.com/clubs/?s=Walsall&post_type=club_societies

Which should then display the desired results ( I think ? ).

This is the form Im currently using on each CPT archive page :

    <div class="et_pb_search et_pb_module et_pb_bg_layout_light et_pb_text_align_left  et_pb_search_0 clear">   
    <form role="search" action="<?php echo site_url('/'); ?>" method="get" class="et_pb_searchform">
        <input type="search" class="et_pb_s" placeholder="<?php echo esc_attr_x( 'Search Clubs...', 'placeholder' ) ?>" value="<?php echo get_query_var('clubs'); ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />
        <input type="hidden" name="post_type" value="clubs" /> <!-- // hidden 'players' value -->
        <input type="submit" class="et_pb_searchsubmit" alt="Search" value="Search" />
     </form>
 </div>

Any help appreciated.

1 Answer 1

3

On the form, change

action="<?php echo site_url('/'); ?>"

To:

action="<?php echo esc_url( get_post_type_archive_link( 'club_societies' ) ); ?>"

Where 'club_societies' is the slug of the post type you're searching.

That will set the form action to the archive URL, with the search parameters added, rather than the homepage with search parameters added.

1
  • 1
    For some reason I thought it was going to be more complicated than that. Thanks alot Jacob that works perfectly. As always im over thinking things.
    – Randomer11
    Dec 11, 2017 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.