Skip Navigation

[Geschlossen] filter date only about days not each hour/timestamp

This support ticket is created vor 10 Monaten, 1 Woche. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Dieses Thema enthält 5 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Nigel vor 10 Monaten.

Assistiert von: Nigel.

Author
Artikel
#2679398
Bildschirmfoto 2024-01-22 um 09.49.51.jpg

Hi,

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:

[wpv-control-postmeta field="wpcf-letzter-termin" type="date" date_format="F j, Y" url_param="wpv-wpcf-letzter-termin" default_label="bitte Datum auswählen" type="select"]

here is the page:
versteckter Link

And here ist the Post, which give me the right timestamp..
https://toolset.com/forums/topic/filter-did-not-work-with-date-field/

#2679462

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

Hi there

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

I'm sorry I don't have a solution for you.

#2679478

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?

best regards - alex

#2679583

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

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.

#2679595

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"..

thx for helping - best regards - alex

#2679764

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

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.

Das Thema „[Geschlossen] filter date only about days not each hour/timestamp“ ist für neue Antworten geschlossen.