Sridhar Katakam

Genesis and WordPress Tutorials

Themes

  • Home
  • About
  • Archive
    • By Child Theme
  • Membership
    • FAQ
  • Forums
    • Members-only
      • Tutorial Requests
    • Paid Support
  • Contact
  • Login

Custom Search Template in Genesis showing Results grouped by Post Types

November 11, 2015 by Sridhar Katakam 30 Comments Favorited: 15 times

Updated on July 23, 2017. Code in search.php has been updated to only show the search result when there is at least 1 matching entry.

A user in Genesis Facebook group asked,

...I want to customize a search results page for a client to show results grouped like:

Article Results

list 5 post/page results or so

Events Results

list 5 CPT events results or so from Events Manager
...

One method in which this can be done is to remove the standard Genesis loop on search result pages and instead add our own which iterates over the specified post types (page, post, recipe etc.) and displays the entries while making sure that we pass the search term to each of the queries.

genesis-search-group-by-post-types

Step 1

Create a file named search.php in the child theme directory having the following code:

< !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

<?php
/**
* Author: Sridhar Katakam
* Link: https://sridharkatakam.com/
*/
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'sk_do_search_loop' );
/**
* Outputs a custom loop.
*
* @global mixed $paged current page number if paginated.
* @return void
*/
function sk_do_search_loop() {
// create an array variable with specific post types in your desired order.
$post_types = array( 'recipe', 'page', 'post' );
echo '<div class="search-content">';
foreach ( $post_types as $post_type ) {
// get the search term entered by user.
$s = isset( $_GET["s"] ) ? $_GET["s"] : "";
// accepts any wp_query args.
$args = (array(
's' => $s,
'post_type' => $post_type,
'posts_per_page' => 5,
'order' => 'ASC',
'orderby' => 'title'
));
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<div class="post-type '. $post_type .'"><div class="post-type-heading">'. $post_type . 's</div>';
// remove post info.
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
// remove post image (from theme settings).
remove_action( 'genesis_entry_content', 'genesis_do_post_image', 8 );
// remove entry content.
// remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
// remove post content nav.
remove_action( 'genesis_entry_content', 'genesis_do_post_content_nav', 12 );
remove_action( 'genesis_entry_content', 'genesis_do_post_permalink', 14 );
// force content limit.
add_filter( 'genesis_pre_get_option_content_archive_limit', 'sk_content_limit' );
// modify the Content Limit read more link.
add_filter( 'get_the_content_more_link', 'sp_read_more_link' );
// force excerpts.
// add_filter( 'genesis_pre_get_option_content_archive', 'sk_show_excerpts' );
// modify the Excerpt read more link.
add_filter( 'excerpt_more', 'new_excerpt_more' );
// modify the length of post excerpts.
add_filter( 'excerpt_length', 'sp_excerpt_length' );
// remove entry footer.
remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_open', 5 );
remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_close', 15 );
remove_action( 'genesis_entry_footer', 'genesis_post_meta' );
// remove archive pagination.
remove_action( 'genesis_after_endwhile', 'genesis_posts_nav' );
// custom genesis loop with the above query parameters and hooks.
genesis_custom_loop( $args );
echo '</div>';
}
}
echo '</div>'; // .search-content
}
function sk_content_limit() {
return '150'; // number of characters.
}
function sp_read_more_link() {
return '... <a class="more-link" href="' . get_permalink() . '">Continue Reading</a>';
}
function sk_show_excerpts() {
return 'excerpts';
}
function new_excerpt_more( $more ) {
return '... <a class="more-link" href="' . get_permalink() . '">Continue Reading</a>';
}
function sp_excerpt_length( $length ) {
return 20; // pull first 20 words.
}
genesis();
view raw
search.php
hosted with ❤ by GitHub

Specify the post types that you would like to display search results from - in your desired order in line 18.

To view the full content, please sign up for the membership.

Already a member? Log in below or here.

 
 
Forgot Password

Filed Under: Genesis, Premium Content Tagged With: search

Recent Comments

  • Sridhar Katakam on BrowserSync in Laravel Valet
  • Bharti on How to add a new website in Google Analytics
  • Chuck Smith on BrowserSync in Laravel Valet
  • arunkumar on BrowserSync in Laravel Valet
  • Craig Simpson on BrowserSync in Laravel Valet
  • Sridhar Katakam on BrowserSync in Laravel Valet
  • arunkumar on BrowserSync in Laravel Valet
  • Vajrasar on BrowserSync in Laravel Valet
  • fatcatdesign on Multiple Rotating Background images with Ken Burns Effect in Altitude Pro
  • admin@heyitsfree.net on How to load Font Awesome 5 in WordPress

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new tutorials by email.

Join 1,042 other subscribers

Categories

Tags

ACF ACF Pro Agency Pro Altitude Pro Background Video Backstretch Centric Pro CPT CSS Grid Digital Pro dynamik equal heights featured image fixed Fixed header Flexbox font awesome footer footer widgets Genesis Sample grid header Infinity Pro Isotope jQuery lightbox Masonry Metro Pro minimum-pro Monochrome Pro Outreach Pro page template parallax Parallax Pro portfolio pre_get_posts responsive search Showcase Pro slider Soliloquy sticky Video Background widget area WooCommerce
Follow @srikat

Recent Posts

  • BrowserSync in Laravel Valet
  • How to dynamically load posts on click using Ajax in Genesis
  • Multiple Rotating Background images with Ken Burns Effect in Altitude Pro
  • Secondary logo on scroll in Business Pro
  • Site Title – Custom Menu – Primary Menu in Genesis
  • Rotating Background Images for Page Header section using Featured Galleries and Backstretch in Business Pro
  • Header Right widget area in Genesis Sample 2.6.0
  • Utility Bar in Genesis Sample
  • CSS Grid with Float Fallback
  • Font Awesome 5 SVG icons above menu items in Genesis

Find my tutorials helpful and wish to donate?

© 2018 · Genesis Sample Theme on Genesis Framework