WP_INSERT_POST自定义分类

时间:2016-11-25 作者:Billy

我有一个自定义的帖子类型books 和自定义分类法books-categories. wp_insert_post() 这是可行的。如何添加第二个名为location? 此代码不保存名为location.谁能帮帮我吗?

<?php
if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "new_post") {

    // Do some minor form validation to make sure there is content
    if (isset ($_POST[\'title\'])) {
        $title =  $_POST[\'title\'];
    } else {
        echo \'Please enter a  title\';
    }
    if (isset ($_POST[\'description\'])) {
        $description = $_POST[\'description\'];
    } else {
        echo \'Please enter the content\';
    }
    $tags = $_POST[\'post_tags\'];

    // Add the content of the form to $post as an array
    $new_post = array(
        \'post_title\'    => $title,
        \'post_content\'  => $description,
        \'post_category\' => array($_POST[\'cat\']),  // Usable for custom taxonomies too
        \'tags_input\'    => array($tags),
        \'post_status\'   => \'draft\',           // Choose: publish, preview, future, draft, etc.
        \'post_type\' => \'books\'  //\'post\',page\' or use a custom post type if you want to
    );
    //save the new post
    $pid = wp_insert_post($new_post); 
    //insert taxonomies
    wp_set_post_terms( $pid, $_POST[\'cat\'], \'books-categories\', false );
    wp_set_post_terms( $pid, $_POST[\'location\'], \'location\', false );
}

?>  

<!-- New Post Form -->
<div id="postbox">
<form id="new_post" name="new_post" method="post" action="">

<!-- post name -->
<p><label for="title">Title</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="title" />
</p>

<!-- post Category -->
<p><label for="Category">Category:</label><br />
<p><?php wp_dropdown_categories( \'show_option_none=Category&tab_index=4&taxonomy=books-categories\' ); ?></p>

<!-- post Location -->
<p><label for="Location">Location:</label><br />
<p><?php wp_dropdown_categories( \'show_option_none=Category&tab_index=4&taxonomy=location\' ); ?></p>

<!-- post Content -->
<p><label for="description">Content</label><br />
<textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
</p>

<!-- post tags -->
<p><label for="post_tags">Tags:</label>
<input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>
<p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>

<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( \'new-post\' ); ?>
</form>
</div>

4 个回复
最合适的回答,由SO网友:C Sabhar 整理而成

wp_set_post_terms() function will only work on the native post type.

对于自定义帖子类型上的分类,请使用wp_set_object_terms().

将代码更改为:

wp_set_object_terms( $pid, $_POST[\'cat\'], \'books-categories\', false );
wp_set_object_terms( $pid, $_POST[\'location\'], \'location\', false );

SO网友:Dan.

问题是您有两个输入,两个输入都用name 属于cat.

您需要更改的第二个实例:

wp_dropdown_categories( \'show_option_none=Category&tab_index=4&taxonomy=location\' )
收件人:

wp_dropdown_categories( \'show_option_none=Category&tab_index=4&taxonomy=location&name=location,\' )
然后更改此选项:wp_set_post_terms( $pid, $_POST[\'cat\'], \'location\', false );

收件人:wp_set_post_terms( $pid, $_POST[\'location\'], \'location\', false );

SO网友:Dipen Desai

try this

//hook into the init action and call create_book_taxonomies when it fires
 add_action( \'init\', \'create_topics_hierarchical_taxonomy\', 0 );

 //create a custom taxonomy name it topics for your posts

 function create_topics_hierarchical_taxonomy() {

    // Add new taxonomy, make it hierarchical like categories
    //first do the translations part for GUI

   $labels = array(
     \'name\' => _x( \'Topics\', \'taxonomy general name\' ),
     \'singular_name\' => _x( \'Topic\', \'taxonomy singular name\' ),
     \'search_items\' =>  __( \'Search Topics\' ),
     \'all_items\' => __( \'All Topics\' ),
     \'parent_item\' => __( \'Parent Topic\' ),
     \'parent_item_colon\' => __( \'Parent Topic:\' ),
     \'edit_item\' => __( \'Edit Topic\' ), 
     \'update_item\' => __( \'Update Topic\' ),
     \'add_new_item\' => __( \'Add New Topic\' ),
     \'new_item_name\' => __( \'New Topic Name\' ),
     \'menu_name\' => __( \'Topics\' ),
  );    

 // Now register the taxonomy

 register_taxonomy(\'topics\',array(\'post\'), array(
  \'hierarchical\' => true,
  \'labels\' => $labels,
  \'show_ui\' => true,
  \'show_admin_column\' => true,
  \'query_var\' => true,
  \'rewrite\' => array( \'slug\' => \'topic\' ),
 ));

}
SO网友:dipak_pusti

wp_set_object_term 是您需要的功能。请参见本页,

https://codex.wordpress.org/Function_Reference/wp_set_object_terms

wp_set_object_terms( $pid, array($_POST[\'cat\']), \'books-categories\' );
wp_set_object_terms( $pid, array($_POST[\'location\']), \'location\' );
希望这个有帮助:)

相关推荐

Custom Taxonomy Page

我正在尝试创建一个显示特定类别的所有子类别的页面。所以在上下文中,我有一个标题为“目的地”的父类别。我希望能够点击目的地,并被引导到一个页面,显示子类别或国家的列表。下面是我试图实现的一个示例,减去顶部的地图-https://www.vagabrothers.com/destinations. 在理想情况下,类别页面的布局将与此页面相同。你可以从上面的例子中看出我的意思。它会使网站上的导航像这样:目的地>国家>个人帖子。我正在使用CPT UI,设置了一个名为“目的地”的自定义帖子类型和类别,然