Skip Navigation

[Resolved] Trying to add a date range options to my view

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

Problem: I would like to use datepickers to set a date range to filter my View

Solution: Views' native date range filters do not utilize datepickers. Instead, they utilize multiple select boxes to set individual date elements, like day, month, year, etc. If you want to use datepickers, you must implement a custom date field in your post type. This field must be automatically updated the first time a post is published. Then this custom field can be used as a date range filter with datepicker style controls.

The following code should be placed in functions.php:

function copy_custom_date_field_on_publish ( $post_id ) {
  // only copy over the publish date on original maintenance-issue 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_on_publish', 1000 );

Relevant Documentation: https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

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

Last updated by nickT-2 7 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#524840

Hi,

I've read some other posts about adding a date range selection to a view's filter. I've tried it and it just says:

'Empty field values or incorrect field defined. | Empty field values or incorrect field defined.'

Here is the code I've added to the filter.

[wpml-string context="wpv-views"][wpv-control field="wpv-post-date" url_param="wpv-post-date_min" type="datepicker" ][/wpml-string]   |   [wpml-string context="wpv-views"][wpv-control field="wpv-post-date" url_param="wpv-post-date_max" type="datepicker"][/wpml-string]

In case it's easier to see the while filter code this is what I've got:

[wpv-filter-start hide="false"]
[wpv-pagination]Showing page: [wpv-pager-current-page style="drop_down"] of [wpv-pager-num-page]  |  [wpv-pager-prev-page][wpml-string context="wpv-views"]Previous[/wpml-string][/wpv-pager-prev-page] [wpv-pager-next-page][wpml-string context="wpv-views"]Next[/wpml-string][/wpv-pager-next-page][/wpv-pagination]
<br><br>
<strong>Change Filter Settings:</strong>
<br>
[wpv-filter-controls]
[wpml-string context="wpv-views"]Department: [/wpml-string] [wpv-control field="department" url_param="department" type="select" auto_fill_default="All" auto_fill="wpcf-department" auto_fill_sort="asc"] <span style="padding-left:20px;padding-right:20px;"> </span> [wpml-string context="wpv-views"]Issue Priority: [/wpml-string] [wpv-control field="issue-priority" url_param="issue-priority" type="select" auto_fill_default="All" auto_fill="wpcf-issue-priority" auto_fill_sort="asc"] <span style="padding-left:20px;padding-right:20px;"> </span>
<br><br>
Sort order date: [wpv-sort-orderby type="select" options="post_date" label_for_post_date="Sort by" orderby_descending_for="post_date"][wpv-sort-order type="radio" options="desc,asc" label_for_asc="Ascending" label_for_desc="Descending" label_asc_for_post_date="Older first" label_desc_for_post_date="Newer first"]
<br>
Select Dates: [wpml-string context="wpv-views"][wpv-control field="wpv-post-date" url_param="wpv-post-date_min" type="datepicker" ][/wpml-string]   |   [wpml-string context="wpv-views"][wpv-control field="wpv-post-date" url_param="wpv-post-date_max" type="datepicker"][/wpml-string]
[/wpv-filter-controls]

[wpv-filter-end]


<br>
<div style="clear: both;"></div>
<hr>

Any ideas what I'm doing wrong. Any help is muchly appreciated. Thanks

#524898

I think the main problem here is that you're trying to filter on post date using the syntax for a custom field. Here's the syntax you're using:

[wpv-control field="wpv-post-date" url_param="wpv-post-date_min" type="datepicker" ]

The field slug wpv-post-date won't work this way. It's not a custom field, it's a native WordPress field, so custom controls don't apply.

Please review the date filtering documentation here:
https://toolset.com/documentation/user-guides/filtering-views-query-by-date/
Use multiple inputs (Year, Month, Day, Hour, etc) to define minimum and maximum dates. This is the preferred method of filtering by date, but it does not allow datepickers.

An alternate method will allow you to use datepickers. Create a new custom field on your posts called "date-range". Set that value automatically whenever the post is published to include today's date. You can use the WordPress save_data hook to handle this. Then you can use datepicker style filters to filter based on this custom field instead of the actual post date. It's a bit convoluted but I can help you set it up if you really would prefer to use datepickers here.

Please let me know if you need additional assistance here.

#525189

Hi Christian,

Looking at the options I think the custom date field and the date picker looks like the best options in terms of it looking the best and probably setting up.

The only challenge is how to quickly copy across the existing wordpress published date to the custom field? Is there a quick way of doing this to avoid manually entering them all?

#525190

Hi Again,

I've set up the custom date field but can't see where I would enter some sort of command like copy data from wordpress date field. You mentioned needing a 'WordPress save_data hook' .. please can I ask how this is done?

#525259

Yes, you'll need to add some custom PHP code to your functions.php file. If you are using a child theme, you can make the modifications in wp-content/themes/your-child-theme-folder/functions.php.

If you're not comfortable editing files, I can help if you give me FTP access. I will enable private reply fields here so you can share in confidence.

I'll also need to know the post type(s) that will include this date range field. If it's a custom post type, I need to know the slug.

#525832

Okay I'm making a clone of your site now so I can perform the necessary changes locally without affecting your live site. Here's what I will do:
- Set up a child theme of your Twenty Fourteen theme so I am not changing the files directly in TwentyFourteen.
- Add the changes to functions.php in the new child theme
- Activate the child theme and verify everything works locally
- Then I will upload the child theme to your live site and activate it with those changes
- Then I will update you that the changes are ready to review

Please standby and I will update you shortly

#525839

That's great support! Thank you
Standing by ;o)

#525859

Hmmm, the tools I have access to aren't working to create a clone. I've seen problems on this hosting platform before, so I suspect they don't allow certain cloning tools. I found this article on wpengine's support site:
hidden link

Could you follow these steps to create a clone, or grant me access so I can do it myself?

#525924

Hi,

Just a thought. I've given you access details to the staging site, which I created for you to test in.

So you should be fine to change anything in the staging site and it not impact the live site.

Is that ok?

#525940

Yes I considered this as well but the FTP credentials you sent don't seem to work for me. Can you double check those? I will enable the private reply fields again.

#525957

Unfortunately I still can't access it, and I'm using port 2222. I asked another supporter here to try the credentials as well and they are unable to access the server. Is it possible there is an IP restriction in place?

#525960

Hi,
Sorry .. for some reasons WPEngine have put the wrong url on the sftp settings.

On the sftp host url can you replace '.....sftp.wpengine.com' with '.....staging.wpengine.com'

And that seems to work for me.

I'm really sorry this is taking a long time to sort

#525964

No, sorry but I still get "Authentication Failed" error. I've tried both active and passive modes, sftp, port 222...any ideas?

#526163
Capture.JPG

Hi
Here is a screenshot of the settings i'm using and managing to connect. I'm using FileZilla

The password is the one beginning with eGa...........

Are your settings the same?

#526165
Capture-intranet-19May2017.JPG

Hi,

Here is a screenshot of the settings I've used and managed to connect ok. I'm using FileZilla.

The password is the one beginning with 'eGa...........'

Just in case - you mentioned port 222 ... in that correct or did you mean 2222 .... it only works with 2222.

Are your settings the same as the screenshot?