Skip Navigation

[Resolved] Use EXIF DateTime as Post DateTime

This support ticket is created 2 years, 2 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/Karachi (GMT+05:00)

This topic contains 8 replies, has 3 voices.

Last updated by Fred Konings 2 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#2268275

Hello support,

What do I want to achieve?
I would like a Custom Post Type “photos” sorted based on DateTime when photos where created.

What have I done so far?
So I have a Custom Post Type called Fotos (Photos). I would like to use the DateTime extracted from the EXIF information of the uploaded photo, to be used as Post date. As a result in my VIEW I am able to sort the photos based on the Post Date.

Here is some code, but I am not sure if this will work?

function update_post_datetime_fotos( $post_id, $form_data ) {
if ( $form_data[ 'id' ] == 485 ) {
if ( function_exists( 'exif_read_data' ) ) {
$exif = exif_read_data( $image->guid );
if ( ! empty( $exif['DateTime'] ) ) {
//var_dump( $exif['DateTime'] );
$post['post_date'] = $exif['DateTime'];
}
}
$updated_datetime = array(
'ID' => $post_id,
'post_date' => $exif,
);
wp_update_post( $updated_datetime );
}
}
add_action( 'cred_save_data', 'update_post_datetime_fotos', 10, 2 );

Could you help me with code above?

#2268457

Nigel
Supporter

Languages: English (English ) Spanish (Español )

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

When you upload images to the WordPress Media Library it creates a post (type 'media') for that image, and stores the available EXIF data in a hidden custom field (with the key '_wp_attachment_metadata' in wp_postmeta).

There is a specific function to retrieve that meta data, namely wp_read_image_metadata (https://developer.wordpress.org/reference/functions/wp_read_image_metadata/), and that meta data (assuming it was available with the uploaded image) contains the timestamp of when the image was created.

So the data will already be available for your uploaded images, it's a question of how best to use it.

How do you have this set up?

You created a custom post type (foto) which has an image custom field and you have a front-end form to add fotos (i.e. the form will publish a foto post), and the form includes the image field.

So when the form is submitted, you want to run some code via the cred_save_data hook to get the timestamp from the image and update the published date of the foto post to match, yes?

That would involve getting the attachment post id (the media library entry) that corresponds to the image field URL, extracting the timestamp of when the photo was taken, converting this to the WordPress post date format, and updating the post date with that value.

Before helping with the code for that, can you confirm it is what you intend to happen?

#2268551

Hi Nigel,

The custom post type (foto) uses the featured image field. The rest of the process you describe is indeed what I am looking for.

Hope you can help me.

#2269015

Updated request:

Hi Nigel,
I have integrated the Mailpoet plugin on my website which automatically generates a weekly newsletter when new posts (photos) are added. Probably Mailpoet does this based on Post date. This mean most likely newsletters will be empty, because photos might me taken much longer ago.

New request: Save EXIF datetime in Custom Field
So maybe it is better I create a new Custom Field called ‘foto_createddatetime’.

So when the form is submitted, I want to run some code via the cred_save_data hook to get the timestamp from the image and update the Custom Field called ‘foto_createddatetime’

That would involve getting the attachment post id (the media library entry) that corresponds to the image field URL, extracting the timestamp of when the photo was taken, converting this to the WordPress post date format (OR to yyyy-mm-dd_hh-mm-ss format) and update the Custom Field called ‘foto_createddatetime’ with that value.

What happens in case EXIF datetime field is empty?
When EXIF datetime field is empty, or cannot be found, the current datetime when form is submitted should be used to update the Custom Field called ‘foto_createddatetime’ with that value.

Hope you can help me.

#2269029

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for sharing this update.

What you're planning seems to be possible and to share some pointers for the custom code, I'll need to perform some tests on my website.

I'll share my findings as soon as this testing completes.

regards,
Waqar

#2271035

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for waiting.

During testing on my website, I was able to make this work, using the following steps:

1. In my actual post type, I added a Toolset date type custom field with slug "post-image-timestamp" and selected "Input date and time" in the field's settings.

2. Next, I used the following function attached to the "cred_save_data" hook, to get the created timestamp from the uploaded featured image and then set it as a custom field value:
( if the image timestamp is not available current timestamp is used )


add_action('cred_save_data', 'custom_update_field_timestamp_func',15,2);
function custom_update_field_timestamp_func($post_id, $form_data) {
	if ($form_data['id']==123) {
		// get featured image ID
		$featured_img_id = get_post_thumbnail_id($post_id);
		// get featured image metadata
		$featured_img_metadata = wp_get_attachment_metadata( $featured_img_id );
		// get featured image created timestamp from metadata
		$featured_img_timestamp = $featured_img_metadata['image_meta']['created_timestamp'];

		if (!empty($featured_img_timestamp)) {
			// if featured image created timestamp is available set it as a custom field value
			update_post_meta($post_id, 'wpcf-post-image-timestamp', $featured_img_timestamp);
		} else {
			// if featured image created timestamp is not available set current time as a custom field value
			update_post_meta($post_id, 'wpcf-post-image-timestamp', time());
		}
	}
}

Note: Please replace '123' and 'post-image-timestamp' with the actual form ID and the custom field slug.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2272787

Thank you for waiting.

During testing on my website, I was able to make this work, with some minor adjustments. Main thing I changed was that I changed time() to current_time('timestamp') to return blog specific timestamp that is set under WP-dashboard->Settings->General.

function custom_update_field_timestamp_func($post_id, $form_data) {
    if ($form_data['id']==485) {
        // get featured image ID
        $featured_img_id = get_post_thumbnail_id($post_id);
        // get featured image metadata
        $featured_img_metadata = wp_get_attachment_metadata( $featured_img_id );
        // get featured image created timestamp from metadata
        $featured_img_timestamp = $featured_img_metadata['image_meta']['created_timestamp'];
 
        if (!empty($featured_img_timestamp)) {
            // if featured image created timestamp is available set it as a custom field value
            update_post_meta($post_id, 'wpcf-post-foto-timestamp', $featured_img_timestamp);
        } else {
            // if featured image created timestamp is not available set current time as a custom field value. changed time() to current_time('timestamp') 
            update_post_meta($post_id, 'wpcf-post-foto-timestamp', current_time('timestamp'));
        }
    }
}
add_action('cred_save_data', 'custom_update_field_timestamp_func',15,2);

Thank you very much for your help! I really appreciate it.

#2273997

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

You're very welcome and glad I could help.

Feel free to mark this ticket as resolved and start a new one for each new question or concern.

#2277117

Thank you for waiting.

We have been testing the solution and solution works as intended.

My issue is resolved now. Thank you very much!

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