WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to automatically create terms in a certain taxonomy when a certain custom post type is published. The newly created term must be the name of the post that was published.

Example: I have a custom post type "country" and a custom taxonomy "country_taxo". When I publish a country say "Kenya" I want a the term "Kenya" to be automatically created under the "country_taxo" taxonomy.

I have accomplished this using the "publish_(custom_post_type) action hook", but i can only get it to work statically. Example:

// This snippet adds the term "Kenya" to "country_taxo" taxonomy whenever 
// a country custom post type is published.

add_action('publish_country', 'add_country_term');
function add_country_term() {
    wp_insert_term( 'Keyna', 'country_taxo');

Like I mentioned above I need this to dynamically add the post title as the term. I tried this, but it doesn't work:

add_action('publish_country', 'add_country_term');
function add_country_term($post_ID) {
    global $wpdb;
    $country_post_name = $post->post_name;
    wp_insert_term( $country_post_name, 'country_taxo');

Does anyone know how I would go about doing this? Any help is greatly appreciated.

share|improve this question
simply replace $wpdb in your function with $post – Bainternet Jun 15 '11 at 12:01
up vote 1 down vote accepted

You're almost there - the problem is you're trying to access the $post object when the function only receives the post ID.

add_action( 'publish_country', 'add_country_term' );
function add_country_term( $post_ID ) {
    $post = get_post( $post_ID ); // get post object
    wp_insert_term( $post->post_title, 'country_taxo' );
share|improve this answer
Thanks @TheDeadMedic. Perfect :) – Duane Jun 15 '11 at 13:16

Your Answer


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.