带有特色图片的wp_insert_post

时间:2014-05-14 05:37:39

标签: php wordpress wordpress-plugin

我使用函数从附加/上传的图片创建帖子。 一切都在完美地运作......我唯一无法弄清楚的变化就是在创建帖子之前使图像成为特色图像。我使用社交海报插件将文章自动发布到社交网络..并且它发布没有图像的文章,因为图像仅在创建帖子后才有特色。请查看下面的代码并帮助我这个问题......

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 );
}

}

我认为这种情况正在发生,因为该功能正在使用

update_post_meta( $new_post_id, '_thumbnail_id', $post_id );

仅在创建帖子后更新图像...

0 个答案:

没有答案