Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active March 1, 2021 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/5ae9026082095e1a856f29b60debf423 to your computer and use it in GitHub Desktop.
Save jchristopher/5ae9026082095e1a856f29b60debf423 to your computer and use it in GitHub Desktop.
<?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( $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