Skip Navigation

[Resolved] Show date created for attached files

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

Problem:

I would need to show a date for the attached files.

Solution:

It needs custom codes, for example:
https://toolset.com/forums/topic/show-date-created-for-attached-files/#post-2339433

Relevant Documentation:

This support ticket is created 2 years, 7 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by luisP-6 2 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#2339423

Hi,

I'm using this code (https://toolset.com/forums/topic/displaying-file-name-title-uploaded-from-admin/) to show file title, but I would need to show a date for the attached files. For example:

TITLE (uploaded 28/03/2022)

Is there any way to show date upload or date creation?

Thank you

#2339433

Hello,

There isn't such kind of built-in feature, you can modify that custom shortcode codes as below:

add_shortcode( 'show_file_info', 'func_show_file_info');
function func_show_file_info($atts) {
   
    global $wpdb;
    $atts = shortcode_atts( array(
        'url' => '',
        'info' => '', // title, alt or id return
    ), $atts);
    $res = '';
    $url = $atts['url'];
    $attachment_id = $wpdb->get_var($wpdb->prepare(
        "SELECT ID FROM $wpdb->posts WHERE guid = %s", $url
    ));
    if($atts['info'] == 'title'){
        $res = get_the_title($attachment_id);
    }
    if($atts['info'] == 'alt'){
        $res = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
    }
    if($atts['info'] == 'id'){
        $res = $attachment_id;
    }
    if($atts['info'] == 'date'){ // add these three lines
        $res = get_the_date( 'd/m/Y', $attachment_id);
    }
    return $res;
}

And use above shortcode like this:
[show_file_info url="[types field='your-field-name' output='raw'][/types]" info="date"]

More help:
https://developer.wordpress.org/reference/functions/get_the_date/

#2339457

My issue is resolved now. Thank you!