Skip to content

Instantly share code, notes, and snippets.

@srikat
Last active July 23, 2017 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save srikat/894f682c8b03443401b0 to your computer and use it in GitHub Desktop.
Save srikat/894f682c8b03443401b0 to your computer and use it in GitHub Desktop.
Custom Search Template in Genesis showing Results Grouped by Post Types. https://sridharkatakam.com/custom-search-template-in-genesis-showing-results-grouped-by-post-types/
// Copy of genesis_custom_loop() with the addition of a line to access Relevanssi's query engine directly
function sk_custom_loop( $args = array() ) {
global $wp_query, $more;
$defaults = array(); //* For forward compatibility
$args = apply_filters( 'genesis_custom_loop_args', wp_parse_args( $args, $defaults ), $args, $defaults );
$wp_query = new WP_Query( $args );
// added this based on http://www.relevanssi.com/knowledge-base/relevanssi_do_query/
relevanssi_do_query( $wp_query );
//* Only set $more to 0 if we're on an archive
$more = is_singular() ? $more : 0;
genesis_standard_loop();
//* Restore original query
wp_reset_query();
}
<?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();
<?php
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'prefix_do_loop' );
/**
* Outputs a custom loop
*
* @global mixed $paged current page number if paginated
* @return void
*/
function prefix_do_loop() {
// get all the post types
$post_types = get_post_types();
// get specific post types in the order given
// $post_types = array( 'recipe', 'page', 'post' );
foreach ( $post_types as $post_type ) {
$s = isset($_GET["s"]) ? $_GET["s"] : "";
// WP_Query arguments
$args = array (
's' => $s,
'post_type' => $post_type,
'posts_per_page' => 5,
'order' => 'ASC',
'orderby' => 'title'
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
echo '<strong>'. $post_type . '</strong>';
while ( $query->have_posts() ) {
$query->the_post();
// do something
echo '<p>' . get_the_title() . '</p>';
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
}
}
genesis();
// custom genesis loop with the above query parameters and hooks
genesis_custom_loop( $args );
// custom genesis loop with the above query parameters and hooks
sk_custom_loop( $args );
.search-content {
background-color: #fff;
padding: 40px;
margin-bottom: 40px;
}
.post-type {
border-bottom: 1px solid #ededed;
margin-bottom: 40px;
}
.post-type:last-child {
border-bottom: none;
margin-bottom: 0;
}
.post-type-heading {
font-size: 40px;
font-size: 4rem;
text-transform: uppercase;
margin-bottom: 40px;
text-align: center;
}
.search .content .entry {
background: transparent;
padding: 0;
margin-bottom: 12px;
}
.search .content .entry-title {
font-size: 20px;
font-size: 2rem;
}
.search .content .entry-title a {
text-decoration: none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment