• Resolved Ahmet

    (@ahmetp83)


    I use Algolia in the backend.

    The problem is the plugin does not understand the filtering by post_type.

    For example:

    Let’s say 5 posts and 2 products are found.

    It shows correctly. 7 results found.

    However, if you filter the search like url/?s=keyword&post_type=product

    Then we see 2 products but it still outputs 7 results found.

    The other problem is that, let’s say the maximum post is 5. It shows pagination even if there are 2 products found. Because it thinks the total count is 7. When you click on page 2, there is nothing.

    If I deactivate the plugin, the pagination and total count work as expected.

    • This topic was modified 2 years, 3 months ago by Ahmet.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter Ahmet

    (@ahmetp83)

    I found the problem with the total count. (I think now pagination also works as expected because the total count is now correct.)

    includes/class-algolia-search.php line: 202 (This hook returns the actual real number of results available in Algolia)

    Removing the found_posts function, solved the problem.

    If is there a better solution than deleting codes, I would like to hear. (remove_filter(‘found_posts’, ‘found_posts’ ); does not work.)

    Thank you for the plugin.

    • This reply was modified 2 years, 3 months ago by Ahmet.
    • This reply was modified 2 years, 3 months ago by Ahmet.
    • This reply was modified 2 years, 3 months ago by Ahmet.
    Thread Starter Ahmet

    (@ahmetp83)

    I thought it is a good solution, but deleting that add_filter causes another problem.

    If the maximum number is 5 (in the reading settings) but there are 8 posts found, it only shows 5 posts. And the output of count is “5 results found”

    It does not show the pagination but you can manually go to the URL /page/2?s=keyword

    So on the second page, you see 2 more posts, and it says “2 results found”

    TL;DR: Deleting add_filter helped to show the correct total number of results when filtering by post_type, but now results cannot exceed the max. number in 1 page.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Pondering this one as I don’t have an answer right now, but I know I’ve seen similar setups recently with trying to filter by post type that way, and it showing inconsistent results. Not sure on a solution for those yet either. I’m not convinced that query argument modifications are truly working the way they may be with standard WP_Query like I know there’s tutorials about.

    Definitely shouldn’t be modifying core WP Search with Algolia code as those modifications would be unset next time we release an update.

    Thread Starter Ahmet

    (@ahmetp83)

    Thank you for the answer. I tried with different WordPress sites and the results are the same.

    This person has the same problem: https://github.com/WebDevStudios/wp-search-with-algolia/issues/225

    https://superstate.no/?s=lmnt (10 pages, each page has 9 posts)

    https://superstate.no/?s=lmnt&post_type=post (fewer posts are found but still 10 pages. Each page has 1 or 2 posts)

    Because it counts all founded posts. Must filter post_type.

    Thread Starter Ahmet

    (@ahmetp83)

    // Store the total number of its, so that we can hook into the found_posts.
    // This is useful for pagination.
    $this->total_hits = $results['nbHits'];

    $results[‘nbHits’] outputs the total number of search items.

    If there are 5 posts + 2 products, it prints 7.

    If we can filter this according to post_type, it will solve the problem I think.

    • This reply was modified 2 years, 3 months ago by Ahmet.
    Thread Starter Ahmet

    (@ahmetp83)

    I made a further investigation and posted here what I found.

    Thread Starter Ahmet

    (@ahmetp83)

    I found a solution to this.

    The plugin uses wp_searchable_posts index for the backend.

    In includes/class-algolia-settings.php there is a function.

    public function get_native_search_index_id() {
    		return (string) apply_filters( 'algolia_native_search_index_id', 'searchable_posts' );
    	}

    We create another function under it.

    public function get_native_search_index_id2() {
    		return (string) apply_filters( 'algolia_native_search_index_id', 'posts_product' );
    	}

    In /includes/class-algolia-plugin.php, we change this:

    $index_id = $this->settings->get_native_search_index_id();
    		$index    = $this->get_index( $index_id );
    
    		if ( null === $index ) {
    			return;
    		}
    
    		new Algolia_Search( $index );
    	}

    to this:

    $index_id = $this->settings->get_native_search_index_id();
    		$index    = $this->get_index( $index_id );
    
    		if ( null === $index ) {
    			return;
    		}
    
    		$index_id2 = $this->settings->get_native_search_index_id2();
    		$index2    = $this->get_index( $index_id2 );
    
    		if ( null === $index2 ) {
    			return;
    		}
    
    		new Algolia_Search($index, $index2 );
    	}

    Now the index is all post types and index2 is only product post.

    In includes/class-algolia-search.php

    We change this

    try {
    			$results = $this->index->search( $query->query['s'], $params, $order_by, $order );
    		} catch ( AlgoliaException $exception ) {
    			error_log( $exception->getMessage() ); // phpcs:ignore -- Legacy.
    
    			return;
    		}

    to this

    
    global $wp_query;   
    $post_type = get_query_var('post_type');   
    if( $wp_query->is_search && $post_type == 'product' )   
    		{   
    			
    		try {
    			$results = $this->index2->search( $query->query['s'], $params, $order_by, $order );
    		} catch ( AlgoliaException $exception ) {
    			error_log( $exception->getMessage() ); // phpcs:ignore -- Legacy.
    
    			return;
    		}
    	}
    	else {
    		try {
    			$results = $this->index->search( $query->query['s'], $params, $order_by, $order );
    		} catch ( AlgoliaException $exception ) {
    			error_log( $exception->getMessage() ); // phpcs:ignore -- Legacy.
    
    			return;
    		}
    
    }

    Now, when you search normally, the results are coming from wp_searchable_posts index. But when you filter the results with post type and post type is the product, the results are coming from wp_posts_product which is only product items are indexed.

    • This reply was modified 2 years, 3 months ago by Ahmet.
    • This reply was modified 2 years, 3 months ago by Ahmet.
    • This reply was modified 2 years, 3 months ago by Ahmet.
    • This reply was modified 2 years, 3 months ago by Ahmet.
    • This reply was modified 2 years, 3 months ago by Ahmet.
    • This reply was modified 2 years, 3 months ago by Ahmet.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    We’re going to need to review and consider this, for sure, before we do any sort of commitment and release, so I can’t promise we’ll for sure do something like this.

    We also may see about still addressing the overall topic, but in a different way, but we haven’t started on that thus far either.

    I don’t think we have any immediate plans for a new release at the moment, so you’re likely going to be safe for a little bit with your own modified version of the plugin.

    Thread Starter Ahmet

    (@ahmetp83)

    Thank you for your attention. I hope you guys will solve this.

    I noted the changes, so I can make it again after the update if this problem exists. ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Wrong search count with “post_type” search’ is closed to new replies.