• Resolved lodestonedynamo

    (@lodestonedynamo)


    Is there a way to allow these pages/post types to show up in the search results? I tried the code mentioned in another post here about pages but it didn’t work. We are using Knowledge Base plugin by Echo Plugins and would like people to also be able to search for help topics on products we sell. Thank you.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author ILLID

    (@mihail-barinov)

    Hi,

    You want to show this pages inside ajax results block or on search results page?

    Regards

    Thread Starter lodestonedynamo

    (@lodestonedynamo)

    both but for sure inside the ajax results block

    Plugin Author ILLID

    (@mihail-barinov)

    Looks like I found the solution for you. Please use following code snippet

    add_filter( 'aws_search_results_all', 'my_aws_search_results_all', 10, 2 );
    function my_aws_search_results_all( $result_array, $s ) {
    
        global $wpdb;
    
        $search_array = array_unique( explode( ' ', $s ) );
        $search_array = AWS_Helpers::filter_stopwords( $search_array );
        $search_terms = array();
        $relevance_array = array();
        $terms_number = 10;
    
        $search_query = '';
        $relevance_query = '0';
    
        if ( is_array( $search_array ) && ! empty( $search_array ) ) {
            foreach ( $search_array as $search_term ) {
                $search_term = trim( $search_term );
                if ( $search_term ) {
    
                    $like = '%' . $wpdb->esc_like( $search_term ) . '%';
    
                    $search_term_len = strlen( $search_term );
                    $relevance = 40 + 2 * $search_term_len;
                    $relevance_desc = 10 + 2 * $search_term_len;
    
                    $search_terms[] = $wpdb->prepare( '( post_title LIKE %s )', $like);
                    $search_terms[] = $wpdb->prepare( '( post_content LIKE %s )', $like);
    
                    $relevance_array[] = $wpdb->prepare( "( case when ( post_title LIKE %s ) then {$relevance} else 0 end )", $like );
                    $relevance_array[] = $wpdb->prepare( "( case when ( post_content LIKE %s ) then {$relevance_desc} else 0 end )", $like );
    
                }
            }
        }
    
        if ( $search_terms ) {
            $search_query = sprintf( ' AND ( %s )', implode( ' OR ', $search_terms ) );
        }
    
        if ( $relevance_array ) {
            $relevance_query = sprintf( ' (SUM( %s )) ', implode( ' + ', $relevance_array ) );
        }
    
        $sql = " 
             SELECT 
                ID, post_title, post_content, post_type, {$relevance_query} as relevance 
             FROM 
                $wpdb->posts 
             WHERE 1 = 1 
                AND post_status = 'publish' 
                AND post_type IN ( 'page', 'epkb_post_type_1' ) 
                GROUP BY ID 
                HAVING relevance > 0 
                ORDER BY relevance DESC, post_date DESC 
             LIMIT 0, {$terms_number}";
    
        $sql = trim( preg_replace( '/\s+/', ' ', $sql ) );
    
        $search_results = $wpdb->get_results( $sql );
    
        if ( ! empty( $search_results ) && !is_wp_error( $search_results ) ) {
            foreach ( $search_results as $result ) {
    
                $image_src = '';
                $post_thumbnail_id = get_post_thumbnail_id( $result->ID );
                if ( $post_thumbnail_id ) {
                    $thumb_url_array = wp_get_attachment_image_src( $post_thumbnail_id );
                    $image_src = $thumb_url_array ? $thumb_url_array[0] : '';
                }
    
                if ( $result->post_type ) {
                    $new_result = array(
                        'name'     => $result->post_title,
                        'link'     => get_permalink( $result->ID ),
                        'image'    => $image_src,
                        'id'       => $result->ID
                    );
    
                    $result_array['tax'][$result->post_type][] = $new_result;
                }
    
            }
        }
    
        return $result_array;
    
    }

    You need to add it somewhere outside the plugins folder. For example, inside functions.php file of your theme or use some plugin for adding code snippets.

    Also after adding this code you will need to go to the plugin settings page and click the ‘Clear cache’ button.

    Thread Starter lodestonedynamo

    (@lodestonedynamo)

    This worked! Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add Pages and Knowledge Base to Search’ is closed to new replies.