Skip Navigation

[Resolved] Searching repeatable fields

This support ticket is created 5 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

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 2 voices.

Last updated by mikeH-3 5 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#1375189

I made a toolset view called 'families' that lists all families we put into WP ('families' is also my custom post type). At the top of this view is a search filter.

I added a search filter to search the 'first name' of family members in a family ('family members' is a repeatable group of custom fields and in that repeatable group is a custom field for FIRST NAME):

[wpv-control-postmeta field="wpcf-first-name" url_param="wpv-wpcf-first-name" placeholder="First Name"]

and hidden link

When I enter a first name of a member in the search filter on frontend of site, the results don't show anything even though it's correct. If I search by post title, it does work so the search itself is working, just not that particular search filter for my custom field in a repeatable group.

I also did create a specific view for 'family members' too to view the repeatable fields properly. This view is inserted into the overall 'families' view. I don't know if that helps or not.

Thanks!

#1375241

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

As I understand you want to add custom search frontend filter for the field "First Name" that belongs to repeating field group you created.

Actually, you can build the custom search and add the frontend filters fields for the post type to which you set the view to query. I see you added another frontend filter field zip code - that should belong to the post type "families" - is that correct?

if you can share access details and problem URL where you added custom search I can review further and 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.

#1376089

Minesh
Supporter

Languages: English (English )

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

To filter the parent view by child post fields, you need to use the view's hook "wpv_filter_query":
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

I've added the following code to "Custom Code" section offered by Toolset:
=> hidden link

add_filter( 'wpv_filter_query', 'func_filter_parent_view_by_child_post_fields', 10, 3 );
function func_filter_parent_view_by_child_post_fields( $query, $view_settings, $view_id ) {

  if( $view_id == 8 and isset($_GET['wpv_view_count']) ) {
    
     $fname = isset($_GET['wpv-wpcf-first-name']) ? $_GET['wpv-wpcf-first-name'] : null;
    
    /// searching for the post ids belongs to first name
    $args = array(
    'post_type' => 'family-members',
    'fields' => 'ids',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'meta_query' => array(
      array(
        'key' => 'wpcf-first-name',
        'value' => $fname,
        'compare' => 'LIKE'
      )
    )
);
 $child_ids= get_posts($args);
    
    // getting related parent based on child id
    $parent = array();
    foreach($child_ids as $k=>$v):
    	$parent[] = toolset_get_related_post( $v,'family-members' );
    endforeach;
    
   // passing found parent post ids
   $query['post__in'] = $parent;
    
    
    
  }
  
 return $query;
}

If you will go to your page where you added the view: hidden link
Try to search by first name and you will see the results now. Can you please confirm it works as expected.

#1376361

That did work! Thanks so much! That was very kind of you to do that.

2 quick questions:
1. How would I add more repeatable fields to that code properly if I want to add them to be filtered? Like if I wanted to add 'birthday' as a search filter.
2. Is this really the only way to search repeatable fields? It seems so easy to add filters for non repeatable fields but I would have never known to do this. Is it possible to make this easier to the average user?

Thanks!

#1376367

3rd Question. When I enter a first name and then click reset, the 'first name' field doesn't clear. The others do. Is that something to do with your code or is it a bug?

New threads created by Minesh and linked to this one are listed below:

https://toolset.com/forums/topic/split-searching-repeatable-fields-reset-button-not-working/

#1376517

4th Question... when I choose a zip code now, all my member entries disappear unless I click on search button.

New threads created by Minesh and linked to this one are listed below:

https://toolset.com/forums/topic/split-searching-repeatable-fields-ajax-filter-not-working/

#1377003

Minesh
Supporter

Languages: English (English )

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

That did work! Thanks so much! That was very kind of you to do that.
===>
Your welcome. Glad to know that the solution that is actually a workaround as there is no native feature yet to filter the view as per your requirement I shared helps.

1. How would I add more repeatable fields to that code properly if I want to add them to be filtered? Like if I wanted to add 'birthday' as a search filter.
===>
You should try to add the same way but as a birthday is the date field - you should try to add it as date picker but again, it needs adjustment with the code I shared that is only applicable to the field "first-name" which is added as a view's filter and that is a part of repeating field groups of "Family Member".

2. Is this really the only way to search repeatable fields? It seems so easy to add filters for non repeatable fields but I would have never known to do this. Is it possible to make this easier to the average user?
===>
Yes - because you want to filter two things using one view. One is fields of repeating field groups of "Family Member" and the fields of "Members" post type where the field you added as filter zipcode belongs to this "Members" post type.

As per our support policy, we entertain only one question per ticket. I'll split the ticket for 3rd and 4th questions. Please feel free to mark resolve this ticket.

#1378673

Minesh
Supporter

Languages: English (English )

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

Can you please mark resolve this ticket as well.

#1378879

My issue is resolved now. Thank you!