Skip Navigation

[Resolved] Search/Order results by Repeating Field Group

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

Last updated by Minesh 1 year, 6 months ago.

Assisted by: Minesh.

Author
Posts
#2597905

Tell us what you are trying to do?
Hi,

I have a custom post type named "events". An event might happen on different days, so it might have multiple start and end dates. To be able to store this, I created a repeating field group that has "start date" and "end date" information on it. You can see how it looks on this page: hidden link (look for "available dates" info).

Now I want to use these start/end dates as search criteria on the search page. I know that RFGs are stored as relationships so we cannot use them directly for search filtering, but I'm looking for a way to be able to do that. Is there a better way to store this information so that I can use them in search?

The same thing is also important for result order. Sometimes I want the result order to be based on those dates as well (here's a sample page where it makes sense to do that: hidden link)

Is there any documentation that you are following?
no

Is there a similar example that we can see?
-

What is the link to your site?
hidden link

#2598561

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

When you create repeating field group, we manage repeating field group as one-to-many post relationship between the current post where repeating field is added (parent) and repeating field posts are treated as child posts.

You can build the custom search view using the post type you set your view to query. If you set "Events" post type to query the view you can only add the filters for the custom fields that is belongs to "Events" post type.

Alternatively, if you set your view to display child post type in your case(repeating field group), then you can add only filters for the custom fields that is belongs to child post type.

So if you want to search for start and end dates, then you will have to set your view to query your child post type (repeating field group) and then add the start and end date filters and display the desired out put accordingly.

#2598581

Hi Minesh,

Thanks for the answer. Unfortunately, I have to search based on other fields on the Event as well. That's why I was asking whether there is a good way to handle this case.

#2598583

Minesh
Supporter

Languages: English (English )

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

Can you please tell me what fields you want to add as custom field filter and what fields you want to display as result? You can build the custom search view with your desired events fields and I will check if I can add the start and end dates as filters.

Also share admin access details and problem URL where you want to display the view with custom search and let me see if I can share you any workaround.

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

#2599093

Minesh
Supporter

Languages: English (English )

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

Thank you for sharing admin access details but we do not recommend to debug the thing on live site.

I see the view you added to the "search-results" page. I see you added the view to display Events post type and added the filters for the custom field belongs to Event post type.

Now, if you want to add start and end date as filters then we will have to add few hooks to remove the start and end date query filers from the Events post type and then grab the start and end date value from the URL param and get the post IDs belongs to the posts that falls within that start and end date but for that we will have to use few filters.

Is it possible for you to setup a staging site? or I can setup a sandbox site where you can setup your Event post type with all fields and repeating field group and then I will work on that sandbox site.

#2599337

Thank you Minesh, unfortunately, my host doesn't support staging and I test all of my changes locally via the Local tool. If there's a way for you to create a sandbox for testing it would be great. I would gladly accept your guidance on how to set something up like that as well

#2599655

Minesh
Supporter

Languages: English (English )

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

Here is a brand new sandbox site where only latest Toolset plugins are installed and you can auto-login to it using the following link:
- hidden link

You can setup all your desired post types, custom fields and repeating field groups and add 5-10 entries and create a view that is set to query the post type where you added the repeating field group to display and then I will check what we can do.

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

#2599865

Thank you for setting that up. I have created the necessary custom post type & fields, added 5 different sample records. Here are the sample records: hidden link

Here's the sample search page: hidden link
What I want to do is to:
1) Add a filter here for the start & end dates in the RFG
2) Sort the results based end end dates in the RFG

#2600165

Minesh
Supporter

Languages: English (English )

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

With the block view, I've added the following filters using the shortcode block for the start and end dates:
=> hidden link

<div class="form-group">
	<label for="wpv-wpcf-start-date">[wpml-string context="wpv-views"]Start Date[/wpml-string]</label>
	[wpv-control-postmeta field="wpcf-start-date" type="date" url_param="wpv-wpcf-start-date"]
</div>
<div class="form-group">
	<label for="wpv-wpcf-end-date">[wpml-string context="wpv-views"]End Date[/wpml-string]</label>
	[wpv-control-postmeta field="wpcf-end-date" type="date" url_param="wpv-wpcf-end-date"]
</div>

Then with the "Custom Code" section I've added the following code:
=> hidden link

add_filter( 'wpv_filter_query', 'func_remove_rfg_field_from_query',10,3 );
function func_remove_rfg_field_from_query( $query_args ,$view_settings,$view_id) {
  
if ($view_id==37) {
  
    foreach((array)$query_args['meta_query'] as $k=>$v):
            if(isset($v['key']) and ($v['key']=='wpcf-start-date' || $v['key']=='wpcf-end-date')){
                 unset($query_args['meta_query'][$k]);
            	}
        endforeach;
   
}
return $query_args;
}

add_filter( 'wpv_filter_query_post_process', 'func_search_rfg_custom_field_return_parent', 10, 3 );
function func_search_rfg_custom_field_return_parent( $query, $view_settings, $view_id ) {
   
  if ( $view_id == 37 ) { 
   	 
    $parent_ids = array();
    if (isset($_GET['wpv-wpcf-start-date']) and $_GET['wpv-wpcf-start-date']!='' and
        isset($_GET['wpv-wpcf-end-date']) and $_GET['wpv-wpcf-start-date']!='' ) {
      
      /// replace with original repeating field group slug
      $rfg_slug='available-dates';
      
      /// getting start and end date values from URL param
      $start_date = $_GET['wpv-wpcf-start-date']; 
      $end_date = $_GET['wpv-wpcf-end-date']; 
      
      $args = array(
                    'post_type'=> $rfg_slug,
                    'meta_query' => array(
                      			array('key' => 'wpcf-start-date',
                                    'type' => 'NUMERIC',
                                    'compare' => '>=',
                                    'value' => $start_date
                      			),
                      			array('key' => 'wpcf-end-date',
                                    'type' => 'NUMERIC',
                                    'compare' => '<=',
                                    'value' => $end_date
                      			))
                );
     
        $date_range_found_posts = get_posts( $args );
     
       $all_posts = $date_range_found_posts;
            
      
      foreach($all_posts as $k=>$v):
     
           $get_parent =  toolset_get_related_post($v->ID,'available-dates');
          
             if(!in_array($get_parent,$parent_ids)){
                    $parent_ids[$get_parent] = get_post($get_parent);
             }else{ 
               unset($all_posts[$k]);
             }
              endforeach;
 
      			
              $query->posts = array_values($parent_ids); 
             $query->found_posts = count($parent_ids); 
              $query->post_count =  count($parent_ids); 
        
               
    }        
  }
    return $query;
}

I can see if I search for specific dates it does return the correct results:
- hidden link

More info:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

#2601317

Thank you Minesh, that's really helpful and seems like it will solve my issue. I'll start digging into your example.

One last question, is there a way to sort the results based on this as well? It feels like if I sort the results on this line, it will do what I'm asking.

$query->posts = array_values($parent_ids);

If so, does that mean the code fetches all records in the database when this filter is running?

#2601365

Minesh
Supporter

Languages: English (English )

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

Can you please try to use the following code and check if that help you to resolve your issue:

add_filter( 'wpv_filter_query_post_process', 'func_search_rfg_custom_field_return_parent', 10, 3 );
function func_search_rfg_custom_field_return_parent( $query, $view_settings, $view_id ) {
   
  if ( $view_id == 37 ) { 
   	 
    $parent_ids = array();
    if (isset($_GET['wpv-wpcf-start-date']) and $_GET['wpv-wpcf-start-date']!='' and
        isset($_GET['wpv-wpcf-end-date']) and $_GET['wpv-wpcf-start-date']!='' ) {
      
      /// replace with original repeating field group slug
      $rfg_slug='available-dates';
      
      /// getting start and end date values from URL param
      $start_date = $_GET['wpv-wpcf-start-date']; 
      $end_date = $_GET['wpv-wpcf-end-date']; 
      
      $args = array(
                    'post_type'=> $rfg_slug,
        			'orderby'=> array('meta_key'=>array('wpcf-end-date'=>'meta_value_num'),
                                       'order'=>'desc'),
                    'meta_query' => array(
                      			array('key' => 'wpcf-start-date',
                                    'type' => 'NUMERIC',
                                    'compare' => '>=',
                                    'value' => $start_date
                      			),
                      			array('key' => 'wpcf-end-date',
                                    'type' => 'NUMERIC',
                                    'compare' => '<=',
                                    'value' => $end_date
                      			))
                );
      
    
           
        $date_range_found_posts = get_posts( $args );
      
        
       $all_posts = $date_range_found_posts;
            
      
      foreach($all_posts as $k=>$v):
     
           $get_parent =  toolset_get_related_post($v->ID,'available-dates');
          
             if(!in_array($get_parent,$parent_ids)){
                    $parent_ids[$get_parent] = get_post($get_parent);
             }else{ 
               unset($all_posts[$k]);
             }
              endforeach;
 
      			
              $query->posts = array_values($parent_ids); 
             $query->found_posts = count($parent_ids); 
              $query->post_count =  count($parent_ids); 
        
               
    }        
  }
    return $query;
}

As you can see I've added the following lines of code to add the orderby statement:

'orderby'=> array('meta_key'=>array('wpcf-end-date'=>'meta_value_num'),
                                       'order'=>'desc'),

You can modify that as per your requirement.

#2602597

That looks great, but I have seen cases where it doesn't work. example: hidden link

I'll debug this a bit to see why but I wanted to update you in the meantime in case you would know the reason.

#2602777

Minesh
Supporter

Languages: English (English )

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

I'm sorry, can you please tell me what exactly not working.

#2603005

I meant the sort doesn't work. If you can take a look at the last link I sent, the results are displayed in the following order (I just wrote the end dates for the records):
- July 28
- July 12
- July 28

#2603217

Minesh
Supporter

Languages: English (English )

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

You can not mix those things. Those items were added using nested view that is used to display the related repeating field group items:
- hidden link

I've set the above view to order the result with start date asc and I can see it shows the correct result. Is this what you expected?