Created
August 10, 2022 15:19
-
-
Save sh-sabbir/bcf8b16a37fe19286304721f9ea88fbd to your computer and use it in GitHub Desktop.
Group and sort search result based on CTP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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