-
-
Save searchwpgists/59bfe4e0a5f65e3cc4e03497d4e01444 to your computer and use it in GitHub Desktop.
SearchWP Live Ajax Search results template to output a custom Source (Users in this case)
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 | |
// Execute a search using our supplemental SearchWP Engine. | |
// @link https://searchwp.com/documentation/knowledge-base/custom-source-results-live-search/ | |
$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 ); ?> » | |
</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 ); ?> » | |
</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