Skip to content

Instantly share code, notes, and snippets.

@sh-sabbir
Created August 10, 2022 15:19
Show Gist options
  • Save sh-sabbir/bcf8b16a37fe19286304721f9ea88fbd to your computer and use it in GitHub Desktop.
Save sh-sabbir/bcf8b16a37fe19286304721f9ea88fbd to your computer and use it in GitHub Desktop.
Group and sort search result based on CTP
<?php
$search_query = new WP_Query(
array(
's' => get_search_query(),
'paged' => $paged,
'post_status' => 'publish'
)
);
// Set CTP priority here. First comes first shows
$types = array( 'post', 'shop', 'restaurant', 'event', 'offer' );
if ( $search_query->have_posts() ):
foreach( $types as $type ) :
while( $search_query->have_posts() ):
$search_query->the_post();
if( $type == get_post_type() ) :
// Render the result item
get_template_part( 'template-parts/search/content-'.$type );
endif;
endwhile;
rewind_posts();
endforeach;
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment