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?
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).
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?
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.
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/
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.