Problem: I would like to get the values of a repeating date custom field, and output a custom data structure including the formatted dates.
Solution:
Custom code is required here. In general, you can get the value of a custom field using the WordPress API get_post_meta. If the Types start date field slug is 'start-tour-date', then the get_post_meta function will look something like this, using a wpcf- prefix for the field slug:
$post_id = 1234; $start_dates = get_post_meta($post_id, 'wpcf-start-tour-date', false);
Note that the Types field slug includes a wpcf- prefix here. This will return an array of date values in Unix timestamp format.
To get a single field value, like the number of tour nights, you can use get_post_meta again like so:
$number_of_tour_nights = get_post_meta($post_id, 'wpcf-number-of-tour-nights, true);
Once you have those values, you can loop over the repeating values and create your own array of information to output. You'll need a way to format dates based on Unix timestamps. You can use PHP's date function for this. For example, to format a date like 2021-08-31 from a Unix timestamp like 1630368000, you can use the date function like so:
$timestamp = 1630368000; $formatted_date = date('Y-m-d', $timestamp);
If you want to calculate a date in the future based on the start date and number of nights, you can use the number of seconds in a day (60 seconds per minute * 60 minutes per hour * 24 hours per day) in your PHP calculations, then format the end date:
$start_date = 1630368000; $number_of_nights = 4; $end_date = $start_date + (60 * 60 * 24 * $number_of_nights); $formatted_end_date = date( 'Y-m-d', $end_date);
Relevant Documentation:
https://developer.wordpress.org/reference/functions/get_post_meta/
https://www.php.net/manual/en/function.date.php
https://toolset.com/documentation/customizing-sites-using-php/functions/
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 5 replies, has 2 voices.
Last updated by 3 years, 10 months ago.
Assisted by: Christian Cox.