Wordpress: insert post-format in post using wp_ins

2019-05-29 14:00发布

as I can insert my php post using the format of a post ( example : post -format- quote ) using 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 );

achievement insert these options but I get no add format post

2条回答
Fickle 薄情
2楼-- · 2019-05-29 14:38

For completeness: there is no need to 'decouple' or have this as a separate operation, as it can be set in the same array with the remaining settings. There is an option (namely, 'tax_input') to set taxonomies directly in the array containing the post parameters.

This is what I use to achieve the same effect:

$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 );
查看更多
smile是对你的礼貌
3楼-- · 2019-05-29 14:47

You need to update post format separately because Post Format is a type of taxonomy. See following example for updating post format.

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

After inserting Post, Post ID is returned. That ID is used to update the post format. In the above example, Image post format will be assigned. Please change it as per your requirement.

查看更多
登录 后发表回答