0

I have a custom post type books and a custom taxonomy books-categories. wp_insert_post() and it works. How can i add a second taxonomy called location? This code doeasn't save my custom taxonomy called location. Can anyone help me please?

<?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>
0

4 Answers 4

2

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

For a taxonomy on a custom post type use wp_set_object_terms().

Change your code to:

wp_set_object_terms( $pid, $_POST['cat'], 'books-categories', false );
wp_set_object_terms( $pid, $_POST['location'], 'location', false );
1
  • I did that but location didn't work again,and on books-categories was created another category called "101".
    – Billy
    Commented Nov 26, 2016 at 18:13
0

The problem is that you have two inputs both sending data with name of cat.

You need to change the second instance of:

wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=location' )

to:

wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=location&name=location,' )

And then change this: wp_set_post_terms( $pid, $_POST['cat'], 'location', false );

to: wp_set_post_terms( $pid, $_POST['location'], 'location', false );

3
  • Still, location taxonomy doesn't work... :/
    – Billy
    Commented Nov 25, 2016 at 16:43
  • Does location appear in the admin panel, in the sub menu underneath the Books menu item in the admin menu?
    – Dan.
    Commented Nov 25, 2016 at 16:53
  • Yes,it appears like books categories...
    – Billy
    Commented Nov 25, 2016 at 19:53
0

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' ),
 ));

}
4
  • No,unfortunately this doesn't work...
    – Billy
    Commented Nov 26, 2016 at 11:14
  • I wonder if the problem is here,maybe i should do the same thing for taxonomy location... $new_post = array( 'post_category' => array($_POST['cat'])
    – Billy
    Commented Nov 26, 2016 at 11:14
  • how to set this code and where to set Commented Nov 26, 2016 at 11:15
  • In array i set ['cat'] as post category, maybe i must set ['location'] also
    – Billy
    Commented Nov 26, 2016 at 11:19
0

wp_set_object_term is the function you need. Please see this page,

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

Hope this one helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.