Orderby Relevanssi
-
Hi
Is there any way to order results by Relevanssi?
I can see that there are the options to do by date, title, name, menu order, random, author, post ID or comment count but it’d be good to order in the way that Relevanssi orders themCheers
Andy
-
Hi Andy,
How does Relevanssi order the posts?Does search results work with Relevanssi and ALM?
Hi
It basically scores terms in the backend in it’s own tables and calculates a score
It then orders by it highest score and uses the returned IDs to do the search e.g.SELECT relevanssi.*, relevanssi.title * 5 + relevanssi.content + relevanssi.comment * 0.75 + relevanssi.tag * 0.75 + relevanssi.link * 0 + relevanssi.author + relevanssi.category * 0.75 + relevanssi.excerpt + relevanssi.taxonomy + relevanssi.customfield + relevanssi.mysqlcolumn AS tf FROM wp_relevanssi AS relevanssi WHERE relevanssi.term = 'term' AND ((relevanssi.doc IN (SELECT DISTINCT(posts.ID) FROM wp_posts AS posts WHERE posts.post_type IN ('post', 'page', 'attachment'))) OR (doc = -1)) ORDER BY tf DESC LIMIT 500Heres a reference guide
http://www.relevanssi.com/category/knowledge-base/Cheers
AndyRight, but does it overtake the standard
's' => $termWP search or does it require its own query?Hi
From what I’m aware of it takes the search parameter runs it’s own query then inserts into the $query->posts
There is a function that can call it directly
relevanssi_do_query()
http://www.relevanssi.com/knowledge-base/relevanssi_do_query/Cheers
AndyHi
Just had a quick look this morning and in the ajax-load-more.php after line 584 I just added this
if ( is_plugin_active( 'relevanssi/relevanssi.php' && $orderby == 'relevanssi' ) ) { relevanssi_do_query( $alm_query ); }and it works for my purpose by passing ‘relevance’ through the orderly parameter. Obviously this is a hack but would be good if this could be added in the shortcode somehow
Cheers
AndySorry that should have said
…by passing ‘relevanssi‘ through…
Cheers
AndyThanks!
I’ll think about how to achieve this in the shortcode. A filter would be easiest but would require more work on the users end.Hi,
Did you figure out a way to add this to the shortcode? I think the template must have changed since this was first posted because inserting that conditional at line 584 is not working for me.
I would really appreciate being able to do this.
Thank!
Hi
With version 2.10.0.1 look around line 1203 in ajax-load-more.php and you will see this
$alm_query = new WP_Query( $args );So straight after this I just put this
if ( ( is_plugin_active( 'relevanssi/relevanssi.php' ) || is_plugin_active( 'relevanssi-premium/relevanssi.php' ) ) && $orderby == 'relevanssi' ) { relevanssi_do_query( $alm_query ); }Then in my shortcode I just pass through ‘relevanssi‘ as the orderby parameter
Whenever you update the plugin you will just need to update this again
What would be good is if after the $alm_query in ajax-load-more.php there was say this
do_action( 'alm_alter_query', $alm_query );That way you could then hook into with say
function alm_sort_by_relevanssi( $query ){ if ( $query->is_search() && isset( $query->query[ 'orderby' ] ) ) { if ( ( is_plugin_active( 'relevanssi/relevanssi.php' ) || is_plugin_active( 'relevanssi-premium/relevanssi.php' ) ) && $query->query[ 'orderby' ] == 'relevanssi' ) { relevanssi_do_query( $query ); } } } add_action( 'alm_alter_query', 'alm_sort_by_relevanssi', 10, 1 );Cheers
AndyHey Andy,
Thanks for getting back to me. After doing exactly that, the posts fail to load on the front and my console log shows an internal error being thrown. Running Relevanssi 3.5.3 and Ajax Load More v2.10.1. I think you meant at line 625 of ajax-load-more.php? Nevertheless, no luck.
I’m certain the condition is being met. But anything put inside there prevents the plugin from loading posts.
Any other ideas? Appreciate your help!
dcooney, some investigation and an action hook would be very helpful. It’s not uncommon for users to use Relevanssi in place of the default WP search.
Yea. I’ll add this my list, as well as SearchWP.
Im busy getting ready for a a release next week but will investigate after that.In the meantime… Is it possible for you to run your Relevanssi loop, store the post ID’s and then pass them to Ajax Load More?
Good idea!
Got it to work. My PHP is not super strong, so this may be crude, but If anyone is interested, here’s what I have in my search.php
global $wp_query; // Detect to see if Relevanssi is active include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); if ( is_plugin_active( 'relevanssi/relevanssi.php' ) || is_plugin_active( 'relevanssi-premium/relevanssi.php' ) ) { // Get the search query $search_query = get_search_query(); // Create a new WP_Query, so you can specify additional parameters (like posts_per_page) $relevanssi_query = new WP_Query(array( 's' => $search_query, 'posts_per_page' => 10 )); // Use the Relevanssi engine to query the posts // using the new search query relevanssi_do_query($relevanssi_query); // Create an array to store the IDs for use in the ALM shortcode $relevanssi_post_ids_arr = array(); if ( $relevanssi_query->have_posts()) : while ($relevanssi_query->have_posts()) : $relevanssi_query->the_post(); // Add the post ID to the array $relevanssi_post_ids_arr[] = $post->ID; endwhile; endif; // var_dump($relevanssi_post_ids_arr); $relevanssi_post_ids = implode(', ', $relevanssi_post_ids_arr); // Echo the shortcode echo do_shortcode("[ajax_load_more post_type='post' post__in='$relevanssi_post_ids']"); }Nice one… Is there a noticed delay in loading posts? Im just wondering how running this extra query might impact the loading times.
No, it appears to load in about the same time. I suppose the user could probably modify the original
$wp_query, but I needed to specify a different posts_per_page than was set globally.Hi
Ahh sorry I was using this version
https://plugins.trac.ww.wp.xz.cn/browser/ajax-load-more/trunk/ajax-load-more.php?rev=1392358
But yes the same placeInstead of using another query you could just use the wp_query and then use a pre_get_posts to control the posts_per_page for the wp_query, would save on the extra query
Cheers
Andy
The topic ‘Orderby Relevanssi’ is closed to new replies.