I have an xml feed populating a custom post type.
I need the custom post type to add the first imported image as the featured image.
I have come across the following function and adapted it:
add_action('add_attachment', 'autoset_featured');
add_action('transition_post_status', 'autoset_featured');
function autoset_featured() {
global $post;
if ($post->post_type == 'property') {
$already_has_thumb = has_post_thumbnail($post->ID);
if (!$already_has_thumb) {
$orderby = 'post_date';
$order = 'ASC';
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&orderby=$orderby&order=$order&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
}
}
}
}
I need it to work so that the featured image is set at the time of the xml import/custom post creation.
I have also tried all the following action hooks and various filters but none have worked:
add_action('add_attachment', 'autoset_featured');
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');
add_action('attachment_fields_to_save', 'autoset_featured');
Currently the featured image is not being set at the time of post creation.
If I open a custom post and click update - it works a dream. But it is unrealistic to have to open and update every post each day.
Any suggestions? The import is set by a cron job. Does anyone know of a cron job to update all posts - I could set this 15 minutes after the original import.