Open kakashigr opened 6 years ago
Hello @kakashigr,
Thanks for opening this issue.
I understand you'd like to use Algolia to take over a custom WP_Query.
The best I can suggest is for you to have a look at our implementation that takes over the main search in the backend: https://github.com/algolia/algoliasearch-wordpress/blob/master/includes/class-algolia-search.php
I would probably try to manually edit the following condition, and dump it to check what the values are when you are loading the additional pages with async calls: https://github.com/algolia/algoliasearch-wordpress/blob/master/includes/class-algolia-search.php#L32
I hope this provides some useful info to get you started.
Let me know how it goes.
Thank you for your response.
I ended up using a different ajax call which gets the /page/2/ of the search results page so the is_main_query() is true and algolia works.
However I'm having some problems with adding advanced search parameters.
I wanted to filter the results by price, so I did this:
$query->set('meta_query', array( array(
'key' => 'product_price',
'value' => array( $pricefrom, $priceto ),
'type' => 'numeric',
'compare' => 'BETWEEN',
)
));
I hooked this to pre_get_posts and it works fine, algolia is correctly returning posts in this price range. However I can't seem to set the order and orderby parameters.
I tried with pre_get_posts but this didn't work. Then I took a look at your source and found two filters for the order and orderby parameters.
This is the code I used, for the orderby:
function filter_algolia_orderby($orderby) {
if ( isset($_GET['gf-sortby-sort']) ) {
$sortby_request = esc_attr($_GET['gf-sortby-sort']);
if ( !empty($sortby_request) ) {
if ( $sortby_request == 'date_desc' ) {
$orderby = 'date';
}
elseif ( $sortby_request == 'date_asc' ) {
$orderby = 'date';
}
elseif ( $sortby_request == 'price_desc' ) {
$orderby = 'meta_value_num';
}
elseif ( $sortby_request == 'price_asc' ) {
$orderby = 'meta_value_num';
}
else {
$orderby = 'date';
}
}
}
return $orderby;
}
add_filter( 'algolia_search_order_by', 'filter_algolia_orderby', 10, 1 );
However, when I try this I get this PHP Fatal Error:
Fatal error: Uncaught RuntimeException: Unable to find replica for attribute "meta_value_num" with order "ASC". in /www/XXXXXXXXXX/public/wp-content/plugins/search-by-algolia-instant-relevant-results/includes/indices/class-algolia-index.php:129
What did you expect to happen?
We have an infinite scroll plugin that fetches page 2, page 3... etc. We have enabled "Use Algolia in the Backend" and the search results page works correctly.
The infinite scroll plugin gets some arguments, if we are on a search page, the search term and which page number we are, in order to generate a custom WP_Query with the correct results. For example:
`$loop_args = array( 'post_type' => 'post', 's' => $squery, 'post_status' => "publish", 'paged' => $paged
); return $loop_args; // ... $results = new WP_Query($loop_args);`
Unfortunately this doesn't use algolia so the results of page 2 are different.
How can we use algolia on a custom WP_Query?
What happened instead?
How can we reproduce this behavior?
Can you provide a link to a page which shows this issue?
Technical info