Skip Navigation

[Resolved] Front end filter by post date range

This support ticket is created 2 years, 4 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 4 replies, has 2 voices.

Last updated by Minesh 2 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#2430427

Tell us what you are trying to do?

I have over 3,000 entries for a custom post type called "Application". I am trying to create a view with a front end filter that will allow me to filter posts by their post creation date. I would like to be able to select posts between 2 dates.

Is there any documentation that you are following?

I have seen other tickets where the advice is to create a custom date post field. With over 3,000 entries already existing this isn't really feasible, unless there is an easy way for all past entries to be populated?

#2430909

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Toolset do not offer to filter posts using the default post date but if you copy the post date to the custom date field you create then its possible.

If you do not know how to do it, I would be happy to help. Can you please share details for what post type you want to filter and share admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) 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.

#2431043

Minesh
Supporter

Languages: English (English )

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

I see there is a date field "Application Date" - do you want to use that field as publish date or should be add a new custom date field namely "application publish date" and copy the publish date to this new date field?

#2431067

Lets use "application publish date" please

#2431117

Minesh
Supporter

Languages: English (English )

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

Rather adding script and new custom date field, I've another workaround. You can check if that works for you.

I've added the following code to "Code snippet" using the "Code snippet" plugin:
=> hidden link

add_filter( 'wpv_filter_query', 'func_filter_applications_by_publish_date', 99, 3 );
function func_filter_applications_by_publish_date( $query_args, $view_settings, $views_id  ) {
	
    if ( $views_id == 64 ) { // if it is specific view and by default
	
		
		if(isset($query_args['meta_query'])) {
		
		$meta_query = $query_args['meta_query'];
		$target_keys = array('wpcf-application-date_from','wpcf-application-date_to');
		
			foreach($target_keys as $k=>$v):
				$found_key = array_search($v, array_column($meta_query, 'key'));
			
				if('wpcf-application-date_from'==$v and $v == $meta_query[$found_key]['key']){
					$s_date = explode("-",date('Y-m-d',$meta_query[$found_key]['value']));
					//$date_query['after'] = array('year'   => $date[0], 'month'  => $date[1], 'day'    => $date[2]);
					
			
				}else if('wpcf-application-date_to' == $v and $v == $meta_query[$found_key]['key']){
							$e_date = explode("-",date('Y-m-d 00:00:00',$meta_query[$found_key]['value']));
					//$date_query['before'] = array('year'   => $date[0], 'month'  => $date[1], 'day'    => $date[2]);
					
				}
				unset($query_args['meta_query'][$found_key]);
			endforeach;
			
			
			$query_args['date_query'] = array(
            			'column' => 'post_date',
          	 			'after' => array('year'   => $s_date[0], 'month'  => $s_date[1], 'day'    => $s_date[2]),
						'before' => array('year'   => $e_date[0], 'month'  => $e_date[1], 'day'    => $e_date[2]),
						'inclusive'  => true );
			
	
		}
		
    }
    return $query_args;
}

I've also added the "From" and "To" date filters to the following view:
=> hidden link


<div class="form-group">
	<label for="wpv-wpcf-application-date_min">[wpml-string context="wpv-views"]From:[/wpml-string]</label>
	[wpv-control-postmeta field="wpcf-application-date_from" type="date" boundary_label_max="From:" url_param="wpv-wpcf-application-date_min"]
</div>
<div class="form-group">
	<label for="wpv-wpcf-application-date_max">[wpml-string context="wpv-views"]To:[/wpml-string]</label>
	[wpv-control-postmeta type="date" field="wpcf-application-date_to"  boundary_label_max="To:"  url_param="wpv-wpcf-application-date_max"]
</div>

Can you please confirm it works as expected: hidden link