Search Knowledge Base by Keyword

This KB documentation is using Advanced Search, Links Editor and Elegant Layouts add-ons.

Print

Advanced Search – Custom Hook

The following hook can be used to integrate an external search into the Advanced Search functionality. The example below is used for Relevanssi.

PHP
				**
  * Integrate Relevanssi search with Echo Advanced Search (ECKB)
  * Allows Relevanssi to handle the actual KB article searching
  */
function integrate_relevanssi_with_epkb( $results, $kb_id, $search_terms, $user_category_ids, $batch_size, $page_num ) {

     if ( ! function_exists( 'relevanssi_do_query' ) ) return null;

     $query = new WP_Query([
         'post_type'      => 'epkb_post_type_' . $kb_id,
         's'              => $search_terms,
         'posts_per_page' => $batch_size,
         'paged'          => $page_num ?: 1,
     ]);

     relevanssi_do_query($query);

    // error_log('🔎 Relevanssi returned ' . count($query->posts) . 'results');

     return $query->posts;
}
add_filter( 'asea_custom_search_results', 'integrate_relevanssi_with_epkb', 10, 6 );

			

The following is a custom solution that has not been tested. Let us know if you have any questions.

PHP
				  /**
   * Integrate SearchWP with Echo Advanced Search (ASEA)
   * Allows SearchWP to handle the actual KB article searching
   */
  function integrate_searchwp_with_epkb( $results, $kb_id, $search_terms, $user_category_ids, $batch_size, $page_num ) {

      // Check if SearchWP is available
      if ( ! class_exists( 'SearchWP' ) ) {
          return null;
      }

      $args = [
          'engine'         => 'default', // or your custom engine name
          's'              => $search_terms,
          'post_type'      => 'epkb_post_type_' . $kb_id,
          'posts_per_page' => $batch_size,
          'paged'          => $page_num ?: 1,
      ];

      // If category filtering is needed
      if ( ! empty( $user_category_ids ) ) {
          $args['tax_query'] = [
              [
                  'taxonomy' => 'epkb_post_type_' . $kb_id . '_category',
                  'field'    => 'term_id',
                  'terms'    => $user_category_ids,
              ]
          ];
      }

      // Perform SearchWP query
      $searchwp_query = new \SearchWP\Query( $search_terms, $args );
      $posts = $searchwp_query->get_results();

      // error_log('🔎 SearchWP returned ' . count($posts) . ' results');

      return $posts;
  }
  add_filter( 'asea_custom_search_results', 'integrate_searchwp_with_epkb', 10, 6 );
			
Was this article helpful?
0 out Of 5 Stars
5 Stars 0%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 0%
5
How can we improve this article?
Please submit the reason for your vote so that we can improve the article.
Need help?
Table of Contents