Creating a CPT search form with results on the same page
-
I just can’t get any results to load and I am really tired and am getting nowhere so I thought I would post seeking assistance and hopefully, after a nap, I will have some clarity.
I have a CPT called
service-providerwith custom taxonomiesregionandservice-categoryI have created a page called Find a service with slug:
find-a-serviceI have created custom page
page-find-a-service.phpwhere I load a search form:<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>"> <div class="form-group"> <input type="hidden" name="post_type" value="service-provider" /> <input type="hidden" name="custom_search" value="true" /> <input type="search" class="search-field form-control" placeholder="<?php echo esc_attr_x( 'Search service providers...', 'placeholder', 'omg' ); ?>" value="<?php echo get_search_query(); ?>" name="s" /> <?php // Taxonomy filters for region $regions = get_terms( array( 'post_type' => 'service-provider', 'taxonomy' => 'region', 'hide_empty' => true, ) ); if ( ! empty( $regions ) && ! is_wp_error( $regions ) ) { ?> <select name="region"> <option value="">Select Region</option> <?php foreach ( $regions as $region ) { ?> <option value="<?= $region->slug ?>"><?= $region->name ?></option> <?php } ?> </select> <?php } ?> <?php // Taxonomy filters for service category $service_categories = get_terms( array( 'post_type' => 'service-provider', 'taxonomy' => 'service-category', 'hide_empty' => true, ) ); if ( ! empty( $service_categories ) && ! is_wp_error( $service_categories ) ) {?> <select name="service-category"> <option value="">Select Service Category</option> <?php foreach ( $service_categories as $service_category ) { ?> <option value="<?= $service_category->slug ?>"><?= $service_category->name ?></option> <?php } ?> </select> <?php } ?> <button type="submit" class="search-submit btn btn-primary"><span class="screen-reader-text"><?php echo _x( 'Search', 'submit button', 'omg' ); ?></span></button> </div> </form>I want the search to land back on the same page and have the results load below the search form so I have created the following filter:
function custom_search_template_redirect() { if ( is_search() && isset( $_GET['custom_search'] ) && $_GET['custom_search'] === 'true' ) { include get_stylesheet_directory() . '/page-find-a-service.php'; // Path to your custom search results template exit; // Stop further execution } } add_action( 'template_redirect', 'custom_search_template_redirect' );however, my code is giving me 0 results – I just can’t get any results to show and I’ve exhausted my will to go on.
The search code below the search form is as follows:
<?php // Get search parameters $search_query = get_search_query(); $region = isset( $_GET['region'] ) ? sanitize_text_field( $_GET['region'] ) : ''; $service_cat = isset( $_GET['service-category'] ) ? sanitize_text_field( $_GET['service-category'] ) : ''; // Perform custom query based on search parameters $args = array( 'post_type' => 'service-provider', 's' => $search_query, 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'region', 'field' => 'slug', 'terms' => $region, ), array( 'taxonomy' => 'service-category', 'field' => 'slug', 'terms' => $service_cat, ), ), ); $query = new WP_Query( $args ); ?> <div id="search-results" class="site-content row" role="main"> <div class="col grid_12_of_12"> <?php if ( $query->have_posts() ) : ?> <header class="page-header"> <h1 class="page-title"><?php printf( esc_html__( 'Search Results for: %s', 'omg' ), '<span>“' . get_search_query() . '”</span>' ); ?></h1> </header> <?php // Start the Loop ?> <?php while ( $query->have_posts() ) : $query->the_post(); ?> <?php get_template_part( 'content', get_post_type() ); ?> <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php else : ?> <p>Use the search fields above to find services.</p> <?php endif; // end have_posts() check ?> </div> <!-- /.col.grid_12_of_12 --> </div> <!-- /#search-results.site-content.row -->Please help me make this work
The topic ‘Creating a CPT search form with results on the same page’ is closed to new replies.