Skip Navigation

[Resolved] Force Relevanssi usage for searches by custom field

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 8 replies, has 4 voices.

Last updated by Minesh 1 year, 2 months ago.

Assisted by: Minesh.

Author
Posts
#2643453

Hi,
I have a search page where users can search by custom fields and also there's a free text search option. Relevanssi plugin is installed on the website. If the free text search is used, Toolset is using the Relevanssi to do the search. I can confirm that with a custom Relevanssi filter I created.

When they go to the search page (hidden link), the visitors see all the listings. The current order is by post date but I want to change it so first "Featured events" are listed, and then "active events" and then the rest. Both featured and active events are handled by custom post fields (the former is a checkbox and the latter is a dropdown).

If the free text option is used, I can easily order them in Relevanssi (I did something similar to this: hidden link). If a visitor just visits the search page or filters by custom post fields only, the Relevanssi filter is not called and order doesn't happen.

I presume I can write a similar code in Toolset to do a similar order (maybe by using "wpv_filter_query_post_process" https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process) but I'm not sure if those two filters would clash.

Is there a way to force the usage of Relevanssi even if I don't use the free text search field? I wouldn't have to worry about keeping two filters up to date and seeing possible unexpected results.

#2643685

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

When you leverage Relevanssi for text searches as a View filter, Views hands over the query including the sorting of the results to Relevanssi, to benefit from its search indexes and relevancy ratings. Filtering by custom fields will use the order settings from your View.

Within the UI you are limited in what you can specify as the ordering options, specifically you can only specify up to two parameters to order by, and only the first parameter can include custom fields, the second parameter can only be standard fields such as post date.

So for your needs you will need to use code, and the correct filter in this case would be the wpv_filter_query hook: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

You will modify the query arguments, which largely correspond to those available with the WP_Query object: https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

It won't interfere with anything you've added to Relevanssi because only one or the other will be operative, depending on whether the text search is being used or not.

#2643785

Thanks for the assistance Nigel. I have 2 follow up questions.

1) I have written a filter like you suggested, but sorting doesn't work correctly. Is there any issue you can identify in the code below? (it seems like it respects the event status custom field (which can have the values ACTIVE or PAST), but it doesn't respect the featured custom field (which can have the values 0 or 1) or the published date.
2) What if I want to randomise the featured events? featured custom field can have the values of 0 or 1, so random order while querying wouldn't work. I can shuffle them after I fetch them from the database, but what is the right way to do it?

Here's the filter function:

function hfk_alter_event_query_and_sort_by_status( $query_args ) {
$types = (array) $query_args['post_type'];
if( in_array( 'event', $types ) ){
unset($query_args['wpv_orderby']);
unset($query_args['wpv_order']);
unset($query_args['order']);

$query_args['meta_query'] = array(
'featured-order-by' => array('key' => 'wpcf-featured'),
'event-status-order-by' => array('key' => 'wpcf-event-status')
);
$query_args['orderby'] = array(
'featured-order-by' => "DESC",
'event-status-order-by' => 'ASC',
'publish_date' => 'DESC'
);
}

error_log("hfk_alter_event_query_and_sort_by_status called. Args: " . json_encode($query_args), 0);

return $query_args;
}

#2644083

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Lets take go through your questions one by one.

It seems you are using "wpv_filter_query" to add your desired custom sorting options.

When you use Relevanssi integration - we just pass the view query to Relevanssi. What if you try to use the following filters in your case:
- hidden link
- https://wordpress.org/support/topic/sort-by-both-custom-field-post_title/#post-15030949

You can check with Relevanssi support for the same.

#2644169

Hi Minesh,

Relevanssi sort is working without an issue as I said in the first message. But I cannot get the sort working for custom post fields when I don't use the text search. My question is about how should I use the "wpv_filter_query". I think it wasn't clear as I didn't add the add_filter method call in my previous message.

#2644263

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I will have to check how exactly you configure you view and where you added the filter and what fields are involved.

Can you please tell me what is your expected result and problem URL where you added your view as well as admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2645127

I have sent the login details in the private post.

My expectation is:
if the user didn't use the text search, but only used other filters on the search page; I want to be able to sort the posts by the "featured" field, then the "event status" field, and lastly the post date. If there's a way to randomize the sort for the featured field, that would be even better.

#2645339

Thank you for sharing these details.

Just wanted to let you know that Minesh is on holiday today.

He'll be back tomorrow and will be able to follow up on this ticket, accordingly.

Thank you for your patience.

#2645499

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've adjusted the code added to your code snippet as given under:

add_filter( 'wpv_filter_query', 'hfk_alter_event_query_and_sort_by_status', 101, 3);
function hfk_alter_event_query_and_sort_by_status( $query_args, $view_settings, $view_id ) {
	
	if($view_id == 1086 and empty($_REQUEST['wpv_post_search']) ) {
		
		
		
$query_args['meta_query'] = array(
    'relation' => 'AND',
    'featured_orderby' => array(
      'key' => 'wpcf-featured',
	  'type'=>'numeric',
      'compare' => 'EXISTS',
    ),
    'event_status_orderby' => array(
      'key' => 'wpcf-event-status',
      'compare' => 'EXISTS',
    ),
      );
 
  $query_args['orderby'] = array(
    'featured_orderby' => 'DESC',
    'event_status_orderby' => 'ASC',
	'post_date' => 'DESC'  
   
  );
 
  unset($query_args['wpv_orderby']);
  unset($query_args['wpv_order']);
		
		
		
	/* 
	 $types = (array) $query_args['post_type'];
	if( in_array( 'event', $types ) ){
		unset($query_args['wpv_orderby']);
		unset($query_args['wpv_order']);
		unset($query_args['order']);
	
		$query_args['meta_query'] = array(
			'featured-order-by' => array('key' => 'wpcf-featured'),
			'event-status-order-by' => array('key' => 'wpcf-event-status')
		);
		$query_args['orderby'] = array( 
			'featured-order-by' => "DESC", 
			'event-status-order-by' => 'ASC', 
			'publish_date' => 'DESC' 
		);
	}   */

	// logging to verify this method is called
	error_log("hfk_alter_event_query_and_sort_by_status called. Args: " . json_encode($query_args), 0);
	}
	
    return $query_args;
}

I set the following event as the featured event:
- hidden link

I can see it displayed as the top. Can you please confirm it works as expected.

#2646285

It works! Thank you so much