Skip to content

Instantly share code, notes, and snippets.

@MjHead
Last active February 5, 2024 14:48
Show Gist options
  • Save MjHead/3597b1d7dba018cc656e2f605fe7cee2 to your computer and use it in GitHub Desktop.
Save MjHead/3597b1d7dba018cc656e2f605fe7cee2 to your computer and use it in GitHub Desktop.
JetSearch. Completely rewrite search query handler
<?php
add_action( 'wp_ajax_jet_ajax_search', 'my_get_search_results', 0 );
add_action( 'wp_ajax_nopriv_jet_ajax_search', 'my_get_search_results', 0 );
function my_get_search_results() {
$request = $_GET['data'];
$search_query = urldecode( $request['value'] );
$per_page = ( int ) $request['limit_query_in_result_area'];
/**
* Here perform your search query
*/
$query = 'resulting query';
if ( 'query is empty' ) {
wp_send_json_success( array( 'message' => $request['negative_search'] ) );
}
// prepare some service data
$limit_query = jet_search_ajax_handlers()->extract_limit_query( $request );
$posts_count = $query->post_count; // you need to extract your results count in some way
$columns = ceil( $posts_count / $limit_query );
$link_target_attr = ( isset( $request['show_result_new_tab'] ) && 'yes' === $request['show_result_new_tab'] ) ? '_blank' : '';
$navigation_settings = array_merge( $request, array(
'limit_query' => $limit_query,
'posts_count' => $posts_count,
'columns' => $columns,
) );
$result = array(
'error' => false,
'post_count' => $posts_count,
'limit_query' => $limit_query,
'columns' => $columns,
'results_navigation' => jet_search_ajax_handlers()->get_results_navigation( $navigation_settings ),
'message' => '',
'posts' => array(),
);
// iterate trough query results to prepare required data
foreach ( $search->posts as $id => $post ) {
/**
* If your $post object in query results is not default WP_Post object, to ensure methods from Jet_Search_Template_Functions will work properly,
* you need to set ID property for post object into actual post ID and post_type property to 'product'
*/
$response['posts'][ $id ] = array(
'title' => $post->post_title,
'before_title' => 'custom data before title',
'after_title' => 'custom data after title',
'content' => 'post contnetnt',
'before_content' => 'custom data before content',
'after_content' => 'custom data after content',
'thumbnail' => Jet_Search_Template_Functions::get_post_thumbnail( $request, $post ),
'link' => 'post link',
'link_target_attr' => $link_target_attr,
'price' => Jet_Search_Template_Functions::get_product_price( $request, $post ),
'rating' => Jet_Search_Template_Functions::get_product_rating( $request, $post ),
);
if ( ! jet_search_ajax_handlers()->has_navigation && $key === $request['limit_query'] - 1 ) {
break;
}
}
wp_send_json_success( $result );
}
@liranop
Copy link

liranop commented Sep 19, 2022

Do you think it would be possible to display terms as a results too?

@bellvision44
Copy link

Hello.
Would it be possible to adapt this code for the latest version of JeSearch? I'd need to filter on the value of a specific custom field, and I confess I'm struggling a bit.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment