Skip Navigation

[Gelöst] Post Field Date Format is not working

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

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 vor 5 Jahre, 9 Monate. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 Antworten, has 2 Stimmen.

Last updated by Adnan vor 5 Jahre, 8 Monate.

Assisted by: Christian Cox.

Author
Artikel
#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.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.