Skip Navigation

[Resolved] Filter by post date

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to include a post date filter in a parametric search View.

Solution: At this time we do not provide a built-in front-end search control for post date. Our developers have accepted a feature request to add this type of filter, but I do not yet have a release date available for that improvement. In the meantime, you have the ability to provide front-end search controls for dates in custom fields, so you could use the save_post API to copy the publish date into a custom field as a timestamp, then insert the custom date field as a filter in your custom search View controls.

Add this custom code to your child theme's functions.php file:

function copy_custom_date_field ( $post_id ) {
  // only copy over the publish date on original posts
  $status = get_post_status( $post_id );
  $pubdate = get_post_meta( $post_id, 'wpcf-issue-date', true);
  if ( $status != 'publish' || $pubdate )
    return;
  $pubdate = get_the_date( 'U', $post_id );
  update_post_meta( $post_id, 'wpcf-issue-date', $pubdate, '' );
}
add_action( 'save_post', 'copy_custom_date_field', 1000 );

Replace "wpcf-issue-date" in both places with the slug of your custom field, using the wpcf- prefix. So if your custom field slug is "custom-post-date" then you should use "wpcf-custom-post-date".

Relevant Documentation:
https://toolset.com/documentation/user-guides/date-filters/
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

This support ticket is created 6 years, 8 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 3 voices.

Last updated by beatR 6 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#673527

Hello,

I would like a date picker for post date.

But when I add a new filter I do not have the option to choose the field "post date" in date picker?

Thanks

#673795

Hi, at this time we do not provide a built-in front-end search control for post date. Our developers have accepted a feature request to add this type of filter, but I do not yet have a release date available for that improvement. In the meantime, you have the ability to provide front-end search controls for dates in custom fields, so you could use the save_post API to copy the publish date into a custom field as a timestamp, then insert the custom date field as a filter in your custom search View controls.

Add this custom code to your child theme's functions.php file:

function copy_custom_date_field ( $post_id ) {
  // only copy over the publish date on original posts
  $status = get_post_status( $post_id );
  $pubdate = get_post_meta( $post_id, 'wpcf-issue-date', true);
  if ( $status != 'publish' || $pubdate )
    return;
  $pubdate = get_the_date( 'U', $post_id );
  update_post_meta( $post_id, 'wpcf-issue-date', $pubdate, '' );
}
add_action( 'save_post', 'copy_custom_date_field', 1000 );

Replace "wpcf-issue-date" in both places with the slug of your custom field, using the wpcf- prefix. So if your custom field slug is "custom-post-date" then you should use "wpcf-custom-post-date".

More information about this here:
https://toolset.com/documentation/user-guides/date-filters/
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

#1204219

I was kind of stumped to not see a create date filter possibility. your code is fine, but what if there are already 10000+ records 🙂 of course, it would be easy to fill the field by code.

bumping this. you got an ETA to be able to filter by default wordpress create/modified date (in a searchform)?