【问题标题】:i want to insert post in specific category using wp_insert_post我想使用 wp_insert_post 在特定类别中插入帖子
【发布时间】:2020-10-22 00:53:32
【问题描述】:

我使用 wp_insert_post 将帖子添加到我创建的自定义帖子类型,但我需要在类别中插入帖子,我使用 post_category=> array($category_id) 但它不起作用.. 这是代码

$my_post = array(
        'post_type' => 'articles',
        'post_title'    => "test",
        'post_content'    => "blabla",
        'post_status'   => 'publish',
        'post_author'   => 1,
        'post_category' => array(38),
    );
    
        wp_insert_post( $my_post );

【问题讨论】:

    标签: php wordpress insert categories posts


    【解决方案1】:

    您可以在帖子创建后设置它:

    $catid = 38;
    wp_set_post_categories($my_post,$catid,false);
    

    如果您不确定类别 ID 是什么,您可以使用类别标签:

    $catid = get_category_by_slug( 'xxx' );
    

    当然,类别分类法应该已经存在于该自定义帖子类型中。 您可以在 CPT 定义中设置它:

    'taxonomies' => array( 'category' ),
    

    【讨论】:

    • $my_post = array('post_type' => 'articles', 'post_title' => "test", 'post_content' => "blabla", 'post_status' => 'publish', ' post_author' => 1, 'meta_input' => 数组('post_id' => $post_id, ) ); $catid = (37); wp_set_post_categories($my_post,$catid,false); wp_insert_post($my_post);我做到了,但也没有用!
    • 如果它不起作用,则类别分类似乎与自定义帖子类型无关,因此您必须编辑自定义帖子类型声明。
    猜你喜欢
    • 2011-09-27
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多