i have a custom Field Datefield with time. Now i wanna filter this, it works like a charm, but i have many Dates with 2 Posts or more with different time in the Datefield. That is reason why all post are as single date/time in Filter. Is there a way to filter only about the date and ignore time? - here ist the filter (yes i use type="select" - this works for me:
I understand that you have a date-time custom field which you are using as a filter for a View. The format of the filter is a select dropdown, where the option values come from the saved values of the custom field. These saved values are based on date and time, though in the select dropdown you only show the date.
Because multiple posts can have the same date but different times, the date options shown in the select dropdown can appear duplicated (even though the actual values are slightly different, because of the different times, not shown).
You want to be able to eliminate the "duplicates".
I have been reviewing the code in our plugin that generates the dropdown filter, looking for somewhere to intervene (via some filter hook) to be able to modify these options.
But, I'm afraid, there is no hook that can be used to modify the generated select options, it is not possible to modify which are included.
You could try using JavaScript to update the select dropdown options after the select input has been rendered, but this might be somewhat complex because the option values I expect will be the stored field values (i.e. timestamps), and so identifying which timestamps refer to the same day will require converting the option values to dates to be able to compare them and then eliminate duplicates.
And a drawback of this would be that in terms of the functioning of the filter itself, you could not use an equals comparison (to return posts with the specified date) because you would not be able to return the posts that correspond to the eliminated "duplicates" (which are not really duplicates, because their values are different).
Hey - thx - an other way could be: i do the custom field from date/time to date only. When i had change this, can i filter in date only? Or is it then -> i must update every single post with the field in it?
If you change the field from date-time to just date the previously saved posts will still have values that reflect date-time, so it would be necessary to re-save all of the affected posts.
I think it should work when quick editing posts, but you should test that before trying it in bulk.
Hi, - thx for your answer. But with Quick-Edit it dont work. I have to edit the post, remove the date and set the new one.. - i have 480 posts - that ist too much for manuel edit.
To Ways:
1. Can i set the filter that it works like "Date between two selections" - like "from date at 0:00 o'clock - to date at 23:59 o'clock"?
or
2. Or second solution: Extrakt DB and edit all Timestamps to "leave the same date and set all Time to same time"..
You could run a simple script to update the existing date to reset it back to the previous midnight (the timestamp used for the date field alone).
If you add a code snippet at Toolset > Settings > Custom Code, rather than activate it you can run it one time only, which is what you want.
The code would be something like this
// get all the relevant posts
$posts = get_posts(array(
'post_type' => 'programm',
'post_status' => 'publish',
'numberposts' => -1
));
foreach ($posts as $post) {
// get current timestamp
$dateandtime = get_post_meta( $post->ID, 'wpcf-letzter-termin', true );
// convert to a date (no time)
$dateString = date('Y-m-d', $dateandtime);
// convert back to a timestamp
$dateonly = strtotime($dateString);
// update field with new value
update_post_meta( $post->ID, 'wpcf-letzter-termin', $dateonly );
}
Naturally, I haven't tested it. Check the code to see if it needs editing (e.g. the slug of the post type).
Be sure to take a backup before you run the code in case it doesn't work as intended.
The topic ‘[Closed] filter date only about days not each hour/timestamp’ is closed to new replies.