Skip Navigation

[Resolved] Post Field Date Format is not working

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to format a timestamp stored in a non-Types custom field, but the wpv-post-field shortcode does not seem to work.

Solution: The wpv-post-field shortcode is meant to be used to display raw values from the database, not formatted values. The Types field shortcode can show formatted dates, but usually not from non-Types fields. You can use a custom shortcode like this:

// format a postmeta field timestamp to return a date
function format_meta_timestamp_date_func($atts) {
  $a = shortcode_atts( array(
    'format' => 'Y-m-d H:i:s',
    'slug' => '',
    'id' => 0
  ), $atts );
 
  $timestamp = get_post_meta( $a['id'], $a['slug'], true );
  if ( !$timestamp )
    return;
  $date = date($a['format'], $timestamp);
  return $date;
}
add_shortcode( 'format-meta-timestamp-date', 'format_meta_timestamp_date_func');

Then use the shortcode like this:

[format-meta-timestamp-date id="12345" format="Y-m-d H:i:s" slug="_sale_price_dates_from"][/format-meta-timestamp-date]

Replace 12345 with the post ID or a wpv-post-id shortcode.

This support ticket is created 6 years, 5 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 2 replies, has 2 voices.

Last updated by Adnan 6 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1071500
toolset-1-woocommerce-field-date.jpg

1) I am trying to: bring this shortcode to work in a views for products

[wpv-post-field name='_sale_price_dates_from' format='Y-m-d H:i:s']

Link to a page where the issue can be seen:
hidden link

The output looks like this below and the shown number should be the formated date.
Gültig vom 1533168000

I expected to see: a formated date

Instead, I got: 1533168000

#1071608

Hi, the wpv-post-field shortcode is for displaying raw values from the database, not formatted dates. The format attribute is not supported here. The Types field shortcode can format a date field, but it looks like this is a non-Types custom field. You might need a custom shortcode for this, something like:

// format a postmeta field timestamp to return a date
function format_meta_timestamp_date_func($atts) {
  $a = shortcode_atts( array(
    'format' => 'Y-m-d H:i:s',
    'slug' => '',
    'id' => 0
  ), $atts );

  $timestamp = get_post_meta( $a['id'], $a['slug'], true );
  if ( !$timestamp )
    return;
  $date = date($a['format'], $timestamp);
  return $date;
}
add_shortcode( 'format-meta-timestamp-date', 'format_meta_timestamp_date_func');

Then use the shortcode like this:

[format-meta-timestamp-date id="12345" format="Y-m-d H:i:s" slug="_sale_price_dates_from"][/format-meta-timestamp-date]

Replace 12345 with the post ID or a wpv-post-id shortcode.

#1074773

Sorry for the delay. Yep. perfect.