WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Auto term creation in a specific taxonomy according to a defined CPT post title (3 posts)

  1. frafor
    Member
    Posted 1 year ago #

    Hi there,

    I have two custom post types, say:

    Labels
    Artists

    Artists has its own taxonomy, called again Label.

    I would like to automatically create a term, within this taxonomy, for each post title present in the CPT Labels.

    In this way, if i create a post titled "My Own Record" in Labels, then i'll get even "My Own Record" as a term within Artists's taxonomy "Label".

    How can i get this? I've made some search, but i couldn't find anything similar.

  2. frafor
    Member
    Posted 1 year ago #

    SOLVED! In theme functions.php :)

    function add_new_term() {
        $args = array(
            'posts_per_page'    =>  -1,
            'post_type'         =>  'labels'
        );
        $title_query = get_posts('numberposts=-1&post_type=labels');
    
    	foreach ($title_query as $single_post) {
    
    		$title = get_the_title($single_post->ID);
    		$taxonomy = 'label';
    		$exist = term_exists($title, $taxonomy);
    
    			if ( $exist == FALSE )
    				{
    				wp_insert_term($title, 'label');
    
    		}
    }
    }
    add_action('init','add_new_term');
  3. frafor
    Member
    Posted 1 year ago #

    Forgot to mark as solved...

Topic Closed

This topic has been closed to new replies.

About this Topic