Wordpress:使用 wp_insert_post() 在 post 中插入 post-格式

Wordpress: insert post-format in post using wp_insert_post()

因为我可以使用 post 的格式插入我的 php post(示例:post -format- quote)使用 wp_insert_post( ).

$my_post = array(
    'post_type'     => 'post', // "post" para una entrada, "page" para páginas, "libro" para el custom post type libro...
    'post_status'   => 'publish', // "draft" para borrador, "future" para programarlo...
    'post_title'    => $_POST['BlogEntranceTitle'], 
    'post_content'  => $_POST['BlogEntranceCode'], 
    'post_author'   => $user_ID, //  
    'post_category' => $_POST['BlogEntranceCats'],  
    'tags_input'    => $_POST['BlogEntranceTags'],
    'post_excerpt'  => $_POST['BlogEntranceExcerpt']
);
wp_insert_post( $my_post );

成就插入这些选项但我没有得到添加格式post

您需要单独更新 post 格式,因为 Post 格式是一种分类法。请参阅以下更新 post 格式的示例。

$my_post = array(
'post_type'     => 'post', // "post" para una entrada, "page" para páginas, "libro"     para el custom post type libro...
    'post_status'   => 'publish', // "draft" para borrador, "future" para programarlo...
    'post_title'    => $_POST['BlogEntranceTitle'], 
    'post_content'  => $_POST['BlogEntranceCode'], 
    'post_author'   => $user_ID, //  
    'post_category' => $_POST['BlogEntranceCats'],  
    'tags_input'    => $_POST['BlogEntranceTags'],
    'post_excerpt'  => $_POST['BlogEntranceExcerpt']
);
$new_post_id  = wp_insert_post( $my_post );
$tag = 'post-format-image';
$taxonomy = 'post_format';
wp_set_post_terms( $new_post_id, $tag, $taxonomy );

插入Post后,返回PostID。该 ID 用于更新 post 格式。在上面的示例中,将分配 Image post 格式。请根据您的要求进行更改。

为了完整性:不需要 'decouple' 或将其作为单独的操作,因为它可以与其余设置一起设置在同一数组中。有一个option(即'tax_input')直接在包含post参数的数组中设置分类法。

这是我用来达到同样效果的:

$my_post = array(
   'post_type'     => 'post'                                     ,
   'post_status'   => 'publish'                                  ,
   'post_title'    => $_POST['BlogEntranceTitle']                ,
   'post_content'  => $_POST['BlogEntranceCode']                 , 
   'post_author'   => $user_ID                                   ,
   'post_category' => $_POST['BlogEntranceCats']                 ,
   'tags_input'    => $_POST['BlogEntranceTags']                 ,
   'post_excerpt'  => $_POST['BlogEntranceExcerpt']              ,
   'tax_input'     => array('post_format' => 'post-format-quote')  //  <- add this to set post_format
);

wp_insert_post( $my_post );