WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Convert Custom Taxonomy to Custom Meta Value (2 posts)

  1. Baden
    Member
    Posted 1 year ago #

    We have a large custom post type with extra custom taxonomies that are used to save info such as 'brand' , 'year' and 'production company'. This worked fine until we introduced multiple languages via WPML. After extensive research, it seems that when a post is duplicated into another language, if there is no translation for the taxonomy the value does not get copied over. For example, if a post has the year taxonomy set to '1972' when it's duplicated to another language, this value is not duplicated. It would duplicate if we created a taxonomy translation of 1972 in every language, but this not logical, as 1972 translates to 1972 in all languages we are using.

    However, if we use a custom meta box, and save 1972 as a custom value, this WILL duplicate over. Therefore the question:

    Does anyone know of a simply method to convert custom taxonomy values to a custom meta value? I am sure we can create a special php function that will do this, but I though I would ask first before hacking a solution from scratch.

    Thoughts? Ideas?

  2. Baden
    Member
    Posted 1 year ago #

    OK, this is what I ended up doing:
    First: create custom template that lists all the post types that need to have the taxonomies converted. While looping each entry I executed the following function:

    $terms_arr = array(
    	'brand' => array('Brand','cv_brand'),
    	'production-company' => array('Production Company', 'cv_prod_co'),
    	'years' => array('Year', 'cv_producton_year')
    );
    foreach($terms_arr AS $key => $value){
    	$terms = get_the_terms( $post->ID, $key );
    	if ( !empty( $terms ) ){
    		$values = array();
    		foreach ( $terms as $term ) {
    			add_post_meta($post->ID, $value[1], strtolower($term->name));
    			echo $post->ID.', '.$value[1].', '.strtolower($term->name).'<br/>';
    			$values[] = strtolower($term->name);
    		}
    		echo $value[0].' ('.$value[1].') : '.join(', ', $values).'<br/>';
    	}
    }

    The $terms_arr is an array in the format of:
    'taxonomy_name' => array('display_name','meta_value_key')

    Finally, create a new page, and assign it the conversion template.
    Visit it once, and it should copy all the custom taxonomies listed in the $terms_arr to the associated custom meta key.

    Hope this helps somebody

Topic Closed

This topic has been closed to new replies.

About this Topic