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
#526167

opps .. sorry for duplicate post

#526723
Screen Shot 2017-05-21 at 11.56.12 AM.png
Screen Shot 2017-05-21 at 11.56.06 AM.png
Screen Shot 2017-05-21 at 11.56.01 AM.png
Screen Shot 2017-05-21 at 11.55.53 AM.png

No problem, I'm using 2222. My settings in FileZilla look like yours for the General tab. Here are my tabs. The only thing I can think is maybe the host has banned my IP for too many failed attempts?

#527826

Hi Christian,

Please could you try with username: gilpinintranet-toolset

That now seems to be working for me. Otherwise all settings on the 4 tabs look the same.

If that still doesn't work is it an option for you to send me modified .php files (renaming the extension to something like .pdf and I can rename .php) and I'll then upload for you?

Thanks

#527983

Still not working for me, unfortunately. I'm not sure what could be the problem. At any rate, I can provide some code for you to implement. Can you tell me:
- What post type(s) will this date range field be applied to? Please provide slugs for each post type.
- What is the slug of your custom date field?
- Should the date field be updated whenever the post is updated, or only on the original publish date?

#527993

Sure thing .. sorry it's not working.

- Post type is called: maintenance-issue
- Custom date field is called: issue-date
- Updated only on the original publish date please

#528108

Okay try this. Add the following code to your theme's functions.php file. If you're not using a child theme, remember that updating your theme will overwrite this function and it will stop working.

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 );

Then try to publish a maintenance issue post from wp-admin. You should see the custom field update to include the current date (and time, if your custom field includes time inputs). Modifying a published post should not trigger an update of this field. You should be able to find the custom field in your sorting and filtering options for Views. Let me know how it goes!

#528484

Hi Christian

Thanks for the code. I've created a child theme and added the code to the functions. I've done a random check on a number of posts and can see the publish date field has copied across to the custom date field. This is great thank you.

Almost there... I've added the date picker code back onto the views filters on this page:

hidden link

(replace the (dot) with a . for the url to work)

It doesn't seem to work very well ... it's a bit slow to show the selected date and the filtered list is showing posts outside the date range I've selected. Maybe my filter code is incorrect which is:

<strong>Date picker:</strong>
from [wpml-string context="wpv-views"][wpv-control field="issue-date" url_param="issue-date_min" type="datepicker" ][/wpml-string]   ... to [wpml-string context="wpv-views"][wpv-control field="issue-date" url_param="issue-date_max" type="datepicker"][/wpml-string]

Here is the view's edit page: hidden link

Thank you so much for your help so far.

#528569
Screen Shot 2017-05-25 at 4.13.15 PM.png

Okay it looks like there was a filter control but no Query Filter, so the control didn't do anything. I've added the appropriate Query Filter for your date range. See the attached screenshot.

This seems to get the date range behaving correctly. Can you confirm this, then I can check on performance?

#528573
After.JPG
Before.JPG

Hi,

Thank that looks like the filter is working great now. But I noticed with the AJAX switched on the Update Form has disappeared.

I've attached a before and after image to show you which bits are missing.

#528583

Okay, I've seen this bug before - CRED forms were not showing up on AJAX-updated Views. As I understand it, this has been resolved in the latest versions of Toolset plugins which were released yesterday. When you have time, I recommend updating CRED, Views, et al to resolve this issue. If you're still missing CRED forms on AJAX requests, please open a separate ticket so we can investigate this issue in more depth. Thanks!

#528589

Hi,

I've updated the plugins and can see the CRED form is still there - so that's fixed.

But after filtering down the post count is still 89. If you filter on 23rd May to 24th May there should be 12 on the post count.

It's also still very slow. Is this the best set of options? Would it be faster if it didn't reload the page?

#528615

Two things about the item count:
- The shortcode you want to use here is [wpv-item-count]
- When you place this shortcode in the filter area, it doesn't update like you might expect it to during an AJAX call. Instead, I recommend placing it in the Loop Output area. You can place it above the <wpv-loop> tag.

I agree, it's painfully slow right now. This isn't normal, but I've seen some strange things on wp-engine. They have their own proprietary caching system, and it seems to slow down some of the calls we make to admin-ajax.php when you're logged in. With Visual Composer enabled, that slowness is exaggerated even more. As a test, I'd like to see what this page performs like when a user isn't logged in...but that would require you open the site up to visitors. The day is ending for me here. I'm off Friday and Saturday but will be back online Sunday to follow up. If you need an earlier response, feel free to open a new ticket and close this one out.

#528818

All working great thank you. It seems much faster on the live site.

Thanks so much for your amazing support!!