Last active
November 13, 2023 16:24
-
-
Save searchwpgists/3d08d22bf23f32e3fda910e17d0bdda0 to your computer and use it in GitHub Desktop.
This file contains 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 | |
// ... this snippet overrides the results output found | |
// @link https://searchwp.com/documentation/search/supplemental-engine/ | |
// so as to account for results that are from a different Multisite site. | |
// This is not a full and complete example, it shows only the customization | |
// to the results output to account for site changes. | |
// Track the current blog ID. | |
$current_blog_id = get_current_blog_id(); | |
?> | |
<?php if ( ! empty( $search_query ) && ! empty( $search_results ) ) : ?> | |
<?php foreach ( $search_results as $search_result ) : ?> | |
<article class="page hentry search-result"> | |
<?php | |
// Track whether we switched sites for this result. | |
$switched_site = false; | |
// Do we need to switch to the proper site for this result? | |
if ( $current_blog_id !== $search_result->site ) { | |
switch_to_blog( $search_result->site ); | |
$switched_site = true; | |
} | |
// Output the result based on SearchWP Source. | |
switch( $search_result->source ) { | |
// Posts and Pages. | |
case 'post' . SEARCHWP_SEPARATOR . 'post': | |
case 'post' . SEARCHWP_SEPARATOR . 'page': | |
$post = get_post( $search_result->id ); | |
?> | |
<header class="entry-header"><h2 class="entry-title"> | |
<a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a> | |
</h2></header> | |
<div class="entry-summary"><?php the_excerpt(); ?></div> | |
<?php | |
wp_reset_postdata(); | |
break; | |
// Users. | |
case 'user': | |
?> | |
<header class="entry-header"><h2 class="entry-title"> | |
<a href="<?php echo get_author_posts_url( $search_result->id ); ?>"> | |
<?php echo esc_html( get_the_author_meta( 'display_name', $search_result->id ) ); ?> | |
</a> | |
</h2></header> | |
<div class="entry-summary"> | |
<?php echo wp_kses_post( get_the_author_meta( 'description', $search_result->id ) ); ?> | |
</div> | |
<?php | |
break; | |
} | |
?> | |
</article> | |
<?php | |
// If we switched sites, switch back! | |
if ( $switched_site ) { | |
restore_current_blog(); | |
} | |
?> | |
<?php endforeach; ?> | |
<?php endif; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment