Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i am currently working on a snippet to insert a new post dynamically (it's a custom post type). During this new post creation i need to insert terms in custom taxonomies associated to the custom post type.

I am having the famous "invalid taxonomy" error message and i don't know how to solve this.

Here is the code i'm using:

  • the custom post type is: property
  • the custom taxonomy is: type

Code:

// Insert property into DB
$property = array(
    'post_title'   => $title,
    'post_content' => $description,
    'post_status'  => 'draft',
    'post_author'  => 1,
    'post_type'    => 'property'
);

// Insert the post into the database
$property_id = wp_insert_post( $property );         

// Taxo Property Type
if( $property_type ) {

    // check if term exists
    $property_type_term = term_exists( $property_type, 'type' );

    if( $property_type_term !== 0 && $term !== null ) {
        // Term exists, get the term id
        $property_type_term_id = $property_type_term;
    } else {
        // Create new term
        $property_type_term_id = wp_insert_term(
                                    $property_type,     // the term 
                                    'type'          // the taxonomy
                                );
    }

    // Assign term id to post
    wp_set_post_terms( $property_id, array($property_type_term_id), 'type' );

}

With this code, the post is correctly created but the term not.

Any help would be much appreciated!

share|improve this question
    
You're likely calling wp_set_post_terms prior to your taxonomy being registered. –  Bainternet Sep 24 '12 at 14:01
    
i'm using this code in a shortcode (inside a plugin), and the taxonomy is registered previously in the plugin. –  Remi Sep 24 '12 at 15:19
    
what is the $property_type refer to? –  ifdion Sep 24 '12 at 22:09
1  
$property_type is just a $_POST[] var not empty. In fact, the code works, i just saw that the bug was in the taxonomy registration, in other words: how to lose time for nothing ! Thanks by the way to those who answered me ! –  Remi Sep 25 '12 at 7:07
    
first you should set the taxnomy in custom post type you have just set only for post title post content post comments you did not set for custom taxnomy 'tax_input' => [ array( <taxonomy> => <array | string> ) ] // For custom taxonomies. Default empty. 'page_template' => [ <string> ] // Requires name of template file, eg template.php. Default empty. ); –  Sri Aug 15 at 12:48

1 Answer 1

first you should set the taxnomy in custom post type you have just set only for post title post content post comments you did not set for custom taxnomy

$post = array('tax_input'      => [ array( <taxonomy> => <array | string> ) ] // For custom taxonomies. Default empty.
  'page_template'  => [ <string> ] // Requires name of template file, eg template.php. Default empty.
);

this query will set the custom taxnomy while creating custom post check this link

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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