I’m using a function to create post from the attached/uploaded images ..
Everything is working perfectly … The only changes i’m not able to figure out is to make the image featured image before the post is created . I use a social poster plugin which post the article automatically to social network.. and it post the article without the image as the image is featured only after the post is created .. Kindly have a look at the code below and help me out with this issue …
public function create_post_from_image( $post_id ) {
if( ! wp_attachment_is_image( $post_id ) )
return;
$new_post_category = array();
// If an image is being uploaded through an existing post, it will have been assigned a post parent
if ( $parent_post_id = get_post( $post_id )->post_parent ) {
/**
* It doesn't make sense to create a new post with a featured image from an image
* uploaded to an existing post. By default, we'll return having done nothing if
* it is detected that this image already has a post parent. The filter allows a
* plugin or theme to make a different decision here.
*/
if ( false === apply_filters( 'afip_post_parent_continue', false, $post_id, $parent_post_id ) )
return;
/**
* If this image is being added through an existing post, make sure that it inherits
* the category setting from its parent.
*/
if ( $parent_post_categories = get_the_category( $parent_post_id ) ) {
foreach( $parent_post_categories as $post_cat )
$new_post_category[] = $post_cat->cat_ID;
}
}
$afip_options = get_option( 'afip_options' );
$current_user = wp_get_current_user();
/* Allow other functions or themes to change the post date before creation. */
$new_post_date = apply_filters( 'afip_new_post_date', current_time( 'mysql' ), $post_id );
/* Allow other functions or themes to change the post title before creation. */
$new_post_title = apply_filters( 'afip_new_post_title', get_the_title( $post_id ), $post_id );
/* Allow other functions or themes to change the post categories before creation. */
$new_post_category = apply_filters( 'afip_new_post_category', $new_post_category, $post_id );
/* Allow other functions or themes to change the post content before creation. */
$new_post_content = apply_filters( 'afip_new_post_content', '', $post_id );
// Provide a filter to bail before post creation for certain post IDs.
if ( false === apply_filters( 'afip_continue_new_post', true, $post_id ) )
return;
// Allow others to hook in and perform an action before a post is created.
do_action( 'afip_pre_create_post', $post_id );
$new_post_id = wp_insert_post( array(
'post_title' => $new_post_title,
'post_content' => $new_post_content,
'post_status' => $afip_options['default_post_status'],
'post_author' => $current_user->ID,
'post_date' => $new_post_date,
'post_category' => $new_post_category,
'post_type' => $afip_options['default_post_type'],
));
if ( isset( $afip_options['default_post_format'] ) && 'standard' !== $afip_options['default_post_format'] )
set_post_format( $new_post_id, $afip_options['default_post_format'] );
update_post_meta( $new_post_id, '_thumbnail_id', $post_id );
// Update the original image (attachment) to reflect new status.
wp_update_post( array(
'ID' => $post_id,
'post_parent' => $new_post_id,
'post_status' => 'inherit',
) );
/**
* Allow others to hook in and perform an action as each operation is complete. Passes
* $new_post_id from the newly created post and $post_id representing the image.
*/
do_action( 'afip_created_post', $new_post_id, $post_id );
}
}
I think this is happening because the function is using
update_post_meta( $new_post_id, '_thumbnail_id', $post_id );
to update the image as featured only after the post is created …