WordPress.org

Ready to get started?Download WordPress

Forums

error: invalid_taxonomy (2 posts)

  1. Toan Nguyen
    Member
    Posted 2 years ago #

    I had setup wordpress with my theme.

    I add taxonomy with below content in funtions.php

    function reg_location(){
    		$args	=	array(
    		    'label' => 'Địa điểm',
    			'hierarchical' => true,
    		    'show_ui' => true,
    		    'query_var' => true,
    			'public' => true,
    			'rewrite' => true,
    		    'rewrite' => array( 'slug' => 'lam-viec' ),
    		);
    		register_taxonomy('noi-lam-viec', array('post','resume'),$args);
    	}
    add_action('init', 'reg_location');

    everything ok but I can't set wp_set_object_terms ?

    $my_post = array(
        	'post_type'	=>'resume',
            'post_title' => $ResumeTitle,
            'post_content' => $ResumeDescription,
            'post_status' => 'publish',
            'post_author' => $current_user->ID,
            'tags_input' => $jobTags,
    		'post_name' => $slug,
     		//'tax_input' => array( 'noi-lam-viec' => array( $mainlocation ) )
        );
    if ($_POST['joblocation']>0) $locationID[] =       $_POST['joblocation'];
    	$tax = 'noi-lam-viec';
    	$demo = wp_set_object_terms($post_id, $locationID, $tax);

    I received error when debug:

    WP_Error Object ( [errors] => Array ( [invalid_taxonomy] => Array ( [0] => Invalid Taxonomy ) ) [error_data] => Array ( ) )

    anyone can help me !

    Thanks

  2. Alex Mansfield
    Member
    Posted 2 years ago #

    I ran across this question while searching for an answer to a similar issue. I eventually found a solution to my issue, and it might help here as well, so I'll try to explain.

    The code in the functions.php file runs before the "init" action hook. This means that if your second block of code placed directly into the functions.php file and is not part of a function called via an action hook, it will run before your first block of code. Setting the object terms before the taxonomy has been created will create problems such as the one you're facing. I can't tell you exactly how to fix it because my situation was a bit different, but you'll need to make sure your second block of code is being run after the first block of code. I believe you should be able to accomplish this by placing the second block of code in a function and attaching it to the init hook. Then you should be able to set the priority of the two functions to ensure that the first one runs first.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.