i want to have filter which allows users to filter the posts by posted date in range.
hidden link
i have seen many old support tickets related to this but they are not working.
Regards
Hi, unfortunately there is no built-in way to filter by post date range like this. The most practical workaround is to create a custom date field on this post type, and use a custom code snippet to set that date field automatically whenever the post is published. Then create the date range filter for the custom field instead of the post publish date.
Here's an example showing how to set a date field automatically:
add_action( 'save_post', 'tssupp_set_date_field', 100, 3 );
function tssupp_set_date_field( $post_id, $post, $update ) {
if ( $post->post_type == 'post' ) {
$date = get_post_meta( $post_id, 'wpcf-date-field-slug', true);
if( $date ) {
return;
}
update_post_meta( $post_id, 'wpcf-date-field-slug', date('U') );
}
}
Let me know if you need more information about this.
Also please note that there seems to be a problem with date range filters in Views 3.0.1, as discussed in your other ticket :
https://toolset.com/forums/topic/filter-by-date-range-for-custom-fieldstype-date-not-working-fine/
Our developers are aware of the issue and working on a fix.
Hi Christian !
Thanks fro explaining me how i can achieve that.
I have got the date range filter working with the hotfix patch provided by your toolset support member Minseh and it working very cool. https://toolset.com/forums/topic/filter-by-date-range-for-custom-fieldstype-date-not-working-fine/
As per your explanation i have created date type custom field and added the code in toolset custom codes section with name of "Entry Date".
I want a custom code or any way that i can copy all the posts published dates automatically (for old and future going to be publish) in custom filed "Entry Date" created.
This way i will be able to store all the posts dates in custom field auto and can apply front end filter easily.
i hope that you understand my requirement to get that working perfectly.
You help will be really appreciated
Regards
Andrew
I want a custom code or any way that i can copy all the posts published dates automatically (for old and future going to be publish) in custom filed "Entry Date" created.
The code I provided before will automatically copy the published date into the custom field. That should happen for any post created in the future. You can test that by creating a new post. If it is not working correctly, please tell me the slug of the post type and the slug of the Entry Date custom field. Then I can show you the changes that should be made in the custom code.
To update the custom field for posts that already exist, I can think of these two options:
1. Write custom code that edits database values directly. I do not recommend this unless you are skilled in SQL development and database management.
2. Resave all existing posts once to trigger the custom code. You can use the bulk-edit feature to select multiple posts and update them all at the same time. See bulk-check.png and bulk-edit.png.
I think #2 is the most practical, if you have just a few posts already. If you have more than a few hundred posts, then #1 is the more reasonable option.