Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active December 11, 2022 07:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/cb9e2151591082be499d47a8d73ad5c8 to your computer and use it in GitHub Desktop.
Save jchristopher/cb9e2151591082be499d47a8d73ad5c8 to your computer and use it in GitHub Desktop.
SearchWP Live Ajax Search results template to output a custom Source (Users in this case) https://searchwp.com/documentation/knowledge-base/custom-source-results-live-search/
<?php
// Execute a search using our supplemental SearchWP Engine.
$search_query = isset( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : null;
$search_results = [];
if ( ! empty( $search_query ) && class_exists( '\\SearchWP\\Query' ) ) {
$searchwp_query = new \SearchWP\Query( $search_query, [
'engine' => 'supplemental', // The Engine name.
'fields' => 'all', // Load proper native objects of each result.
] );
$search_results = $searchwp_query->get_results();
}
?>
<?php if ( ! empty( $search_query ) && ! empty( $search_results ) ) : ?>
<?php foreach ( $search_results as $search_result ) : ?>
<?php
switch( get_class( $search_result ) ) {
case 'WP_Post':
?>
<div class="searchwp-live-search-result" role="option" id="" aria-selected="false">
<p><a href="<?php echo esc_url( get_permalink( $search_result->ID ) ); ?>">
<?php echo get_the_title( $search_result->ID ); ?> &raquo;
</a></p>
</div>
<?php
break;
case 'WP_User':
?>
<div class="searchwp-live-search-result" role="option" id="" aria-selected="false">
<p><a href="<?php echo get_author_posts_url( $search_result->data->ID ); ?>">
<?php echo esc_html( $search_result->data->display_name ); ?> &raquo;
</a></p>
</div>
<?php
break;
}
?>
<?php endforeach; ?>
<?php else : ?>
<p class="searchwp-live-search-no-results" role="option">
<em><?php esc_html_e( 'No results found.', 'swplas' ); ?></em>
</p>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment