• Having some big issues since I’m having problems with defining the right post type: Here’s the code

    function attach_post_cat(){
    	$title = single_post_title('',false);
    
            //These two lines of code doesn't work
    	//$queried_post_type = get_query_var('post_type');
    	//if ( 'productcategory' == $queried_post_type){
    		wp_insert_term(
    			$title,
    			'productcategory',
    			array(
    				'slug' => $title,
    				'parent' => false
    			)
    		);
    	}
    }
    
    add_action('save_post', 'attach_post_cat');

    It works now that whenever I create a page, post or a custom post-type it adds a term with the same name. Super! (not really). I only need it to add a term when I create a product-category.

    Anyone know how to find the right post type?

Viewing 3 replies - 1 through 3 (of 3 total)
  • I think the problem you have is that you’re asking for a new term to be created each time your callback runs..

    What you’re probably wanting to do, is to associate a term with post, for updating a post’s(any type) assigned terms, see wp_set_post_terms.

    http://codex.wordpress.org/Function_Reference/wp_set_post_terms

    Thread Starter jocken

    (@jocken)

    wp_set_post_terms assigns a term. I don’t want to assign a term. Just create it, the assignment is for the end user. I wan’t to do this because the site needs taxonomies that are more detailed and need more fields.

    And it’s not possible without hacking the database, and the plugins are not that great. So I thought it would be easier to just create a term when a (custom post type) product category is created.

    So when I write ‘animal’ in the title field of product category it should also create a term inside the (custom) taxonomy product category called ‘animal’.

    So why not call get_term first to see if the term exists, and if the term doesn’t exist then create it, if it does then stop executing code..

    Basic example in code form so you know what i’m getting at..

    $my_term = get_term( 'the_term_to_fetch', 'taxonomy_name' );
    if( is_null( $my_term ) ) {
      // The term does not exist - execute term creation code
    }
    else {
      // The term already exists - stop executing, return(or whatever)
    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Creating a post will create a term?’ is closed to new replies.