Skip Navigation

[Resolved] Relevanssi doesn't find data in the parent post type fields

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 9 replies, has 2 voices.

Last updated by Minesh 1 month ago.

Assisted by: Minesh.

Author
Posts
#2778108

Tell us what you are trying to do?
- I am trying to use Relevanssi text search to find data in custom fields of the parent post type.
- Parent post has a many-to-many relationship type with a child post type.
- The text search is located in the view of the child post type.
- The child post type view has a parent post type nested view.

- I checked that the search is enabled for required parent post fields.
- Parent post fields are added to the Relevanssi index
- Relevanssi sees indexed fields and its post lookup by ID shows the required fields' values, so it sees custom fields data.
- I tried to reduce the minimum word length for a search using this guide just in case: hidden link

Is there any documentation that you are following?
- yes, this one: https://toolset.com/course-lesson/searching-texts-in-custom-fields-with-toolset-and-relevanssi/?utm_source=plugin&utm_medium=gui&utm_campaign=toolset

Is there a similar example that we can see?
- I am not sure if I know about a site that uses the search in a similar way

What is the link to your site?
- hidden link

#2778157

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - normally, the text search should search on the fields that belongs to post type you set your view to query.

So, for example, if you have post type A (parent) with custom fields (aa,bb,cc) and post type B (child) with custom fields (dd,ee,ff) in many-to-many relationship and if you build the view to query post type B (child) then it should apply search on fields belongs to post type B that is fields (dd,ee,ff).

If you can share problem URL and admin access details and tell me what are those fields you want to target and see and what text string I should use to search and what is your expected result. Once I review all those required information I will be able to guide you in the right direction.

*** 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.

#2778341

Minesh
Supporter

Languages: English (English )

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

I checked the view you created and the issue you have.

As shared before:

Well - normally, the text search should search on the fields that belongs to post type you set your view to query.

So, for example, if you have post type A (parent) with custom fields (aa,bb,cc) and post type B (child) with custom fields (dd,ee,ff) in many-to-many relationship and if you build the view to query post type B (child) then it should apply search on fields belongs to post type B that is fields (dd,ee,ff).

To workaround it I tried few hooks and I get the desired post but somehow post__in argument is not working with Relevanssi.

You can check the different hook I try and try to hook in the post__in argument in order to get your desired result under the "Custom Code" section offered by Toolset:
=> hidden link

//add_filter( 'relevanssi_modify_wp_query', 'func_rlv_adjust_search' );
function func_rlv_adjust_search( $query ) {
  

  
  if(isset($_GET['wpv_post_search']) and $_GET['wpv_post_search']!='') {
  
    remove_filter( 'relevanssi_modify_wp_query', 'func_rlv_adjust_search' );
  
   $args  = array(
    's'           => $_GET['wpv_post_search'],
    'post_types'  => array('person'),
    'fields' => 'ids',
    'relevanssi'  => true,
  );

$person_query = new WP_Query( $args );
    
    
    
  $args  = array(
    's'           => $_GET['wpv_post_search'],
    'post_types'  => array('internment'),
    'fields' => 'ids',
    'relevanssi'  => true,
  );

$internment_query = new WP_Query( $args );
  
  $parent_person_posts = array();
  foreach($internment_query->posts as $k=>$v):
     $parent_person_posts[] = toolset_get_related_post($v,'person-to-internment');

  endforeach;
 
  $final_res = array_merge($person_query->posts,$parent_person_posts);

    
 
   $query->query_vars['post__in'] =  join(",",$final_res);
  // relevanssi_do_query( $query );
  add_filter( 'relevanssi_modify_wp_query', 'func_rlv_adjust_search' );
     $query->query_vars['post__in'] =  join(",",$final_res);
    $query->set('post__in',join(",",$final_res));
    
 

    
}

  
 
//  return $query;
}


//add_filter( 'relevanssi_results', 'rlv_rating_weights');
function rlv_rating_weights( $results ) {

  remove_filter( 'relevanssi_results', 'rlv_rating_weights');
   $internment_parents = do_shortcode('[wpv-view name="get-related-internment-posts" cached="off"]');
   $internment_parents = explode(",",trim( $internment_parents));
   $final = array_merge($results,$internment_parents);
  
  
  $final_results = array();
  foreach($final as $k=>$v):
  	$final_results[$v] = 1;
  
  endforeach;
  //add_filter('relevanssi_results', 'rlv_rating_weights');
  

   
  
 
  return $final_results;
}

Can you please get in touch with Relevanssi support and check with them how we can hook in the post__in argument using the above hook.

#2778730

Hi Minesh, thank you a lot. I will get in touch with them and let you know

#2778747

Minesh
Supporter

Languages: English (English )

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

Ok fine - please let me know that would help others as well.

#2778755

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

You shared:

- the text search is set to find data in Persons post type fields "Given name and surname" and in Internment post type fields "POW#, Year from of internment" and "Year to of internment".
- type "363" or "1916" in the search field (this is a POW# and year of internment for Stusiak Alek post.

I've created the following view to search in internment post type custom fields and the following view return the associated post's parent post ID from the view's loop. As you can see with the following filter "relevanssi_hits_filter" hook where we used this view:
- hidden link

I've added the following filter hook to "Custom Code" section offered by Toolset with the code snippet "relevanssi-roll-call-view":
=> hidden link

add_filter( 'relevanssi_hits_filter', 'func_search_child_field_relevanssi_on_parent_view' );
function func_search_child_field_relevanssi_on_parent_view( $hits ) {
  
   remove_filter( 'relevanssi_hits_filter', 'func_search_child_field_relevanssi_on_parent_view');
   $result = do_shortcode('[wpv-view name="get-related-internment-posts" cached="off"]');
   add_filter( 'relevanssi_hits_filter', 'func_search_child_field_relevanssi_on_parent_view');
  
  if(!empty($result)){
    $result = explode(",",trim($result));
    $internment_posts = array();
    
    foreach($result as $k=>$v):
    	$internment_posts[] = get_post($v);
    endforeach;
    $hits[0] = array_merge($hits[0],$internment_posts);
 }
  
  
  
  return $hits;
}

Can you please confirm it works as expected.

#2778875

Hi Minesh, thank you a lot for your help.
I deactivated the Toolset code snippet "relevanssi-search-word-length" and activated "relevanssi-roll-call-view". I tried to do a search but I didn't receive any results for internment post field values. But it worked for people fields as it worked previously.
I also didn't spot any errors in the PHP error log file.
Please let me know if you should do tests differently.

I also asked Relevanssi your question on their support forum.

I am thinking at this point of removing the internment post type at all and just adding custom fields in the person's post. It's kind of an ugly solution as I would need to create the same fields 4 times to accommodate persons with multiple internments, but it should work in this case as far as I understand.

#2778984

Minesh
Supporter

Languages: English (English )

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

I am thinking at this point of removing the internment post type at all and just adding custom fields in the person's post. It's kind of an ugly solution as I would need to create the same fields 4 times to accommodate persons with multiple internments, but it should work in this case as far as I understand.
===>
Yes - that should work and that will be the best solution as per this situation and you will have to depend on lesser workarounds/filter hooks.

Can you please check now:
- hidden link

I've added the code to "relevanssi-roll-call-view.php" code snippet:
=> hidden link

add_filter( 'relevanssi_hits_filter', 'func_search_child_field_relevanssi_on_parent_view' );
function func_search_child_field_relevanssi_on_parent_view( $hits ) {
   
   remove_filter( 'relevanssi_hits_filter', 'func_search_child_field_relevanssi_on_parent_view');
   $result = do_shortcode('[wpv-view name="get-related-internment-posts" cached="off"]');
   add_filter( 'relevanssi_hits_filter', 'func_search_child_field_relevanssi_on_parent_view');
   
  
  if(!empty($result)){
    $result = explode(",",trim($result));
    $internment_posts = array();
     
    foreach($result as $k=>$v):
        $internment_posts[] = get_post($v);
    endforeach;
    
   
    $hits[0] = array_merge($hits[0],$internment_posts);
 }
   
   
   
  return $hits;
}

If I search for "363" or "1916" in the search field I can see the Stusiak Alek post as result.

#2779099

Hi Minesh thank you for your reply and phenomenal support as always. Indeed the search works well and returned related person posts to search values from the internment post. Interestingly when you search for "Alek" it returns the same post two times.
Please don't feel like you have to resolve this as well because I would most likely move to remove internment posts at all (thank you for your opinion on this) just to simplify things and make the view easier to maintain in the future.

I am keeping this ticket open for a little just to provide a Relevanssi support response to your question related to the initial solution you created.

#2779176

Minesh
Supporter

Languages: English (English )

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

Ok - fine.

#2779254

Thank you again for the great support