WordPress.org

Ready to get started?Download WordPress

Forums

CPT-onomies: Using Custom Post Types as Taxonomies
Feature : Better integration with WPML (12 posts)

  1. sireneweb
    Member
    Posted 2 years ago #

    Hi,
    actually,
    when you create for each language an items and you attach your custom post type as taxomony with other post type,if your translations are same in other language it display all translations but no information to know who language using.

    for exemple :
    checkbox - item 1, name : fanless
    checkbox - item 2, name : fanless
    checkbox - item 3, name : fanless

    better if we have :
    checkbox - item 1, name : fanless (UK)
    checkbox - item 2, name : fanless (ES)
    checkbox - item 3, name : fanless (DE)

    http://wordpress.org/extend/plugins/cpt-onomies/

  2. sireneweb
    Member
    Posted 2 years ago #

    or in your function "CPTonomy_Walker_Terms_Checklist", display only the taxomony items with the current language.

    if we are in english translation for an items, display in all tab only related items from the current language. actually you display all items for each language in CPTonomy_Walker_Terms_Checklist.

    i used this to get the name of the translations

    function get_language_for_element_w($element_id, $el_type='post_post'){
            global $wpdb;
            return $wpdb->get_var("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_id='{$element_id}' AND element_type='{$el_type}'");
        }
  3. ffto
    Member
    Posted 2 years ago #

    Hi sireneweb,

    I had the same problem, so I added this code to my functions.php:

    function filter_cptonomies_get_terms ( $terms, $taxonomies, $args ){
    	global $sitepress;
    	$post_types = get_post_types();
    	foreach ( $terms as $iTerm => $term ){
    		if ( in_array( $term->taxonomy , $post_types ) ){
    			$post	= get_posts( 'post_per_page=1&post_type='.$term->taxonomy.'&name='.$term->slug );
    			$lang	= $sitepress->get_language_for_element($post[0]->ID, "post_{$term->taxonomy}" );
    			$term->name = "{$term->name} ({$lang})";
    		}
    	}
    	return $terms;
    }
    add_filter( 'get_terms' , 'filter_cptonomies_get_terms' , 20 , 3 );

    I would have preferred showing only the current language cpt-onomies, but when you change the language of the post (in the Language metabox), it reloads the Category taxonomy, but not the CPT-onomies taxonomies.

    I'm sure I could figure out what jQuery function gets triggered to reload the default taxonomy, but adding the language to the term name is good enough I think.

  4. AlexNowak
    Member
    Posted 2 years ago #

    Hey ffto!

    Great work. Thank you!
    As long as it's not possible to show the selected language only I'll go with your solution.

  5. ffto
    Member
    Posted 2 years ago #

    here's an updated version :

    function filter_cptonomies_get_terms ( $terms, $taxonomies, $args ){
    	global $sitepress;
    	$post_types = get_post_types();
    	foreach ( $terms as $iTerm => $term ){
    		if ( in_array( $term->taxonomy , $post_types ) ){
    			$post		= get_posts( 'post_per_page=1&post_type='.$term->taxonomy.'&name='.$term->slug );
    			$lang		= strtoupper( $sitepress->get_language_for_element($post[0]->ID, "post_{$term->taxonomy}" ) );
    			$current	= strtoupper( ICL_LANGUAGE_CODE );
    
    			if ( is_admin() ){
    				$term->name = "{$term->name} ({$lang})";
    			}elseif ( $lang !== $current ) {
    				unset( $terms[$iTerm] );
    			}
    		}
    	}
    	return $terms;
    }
    add_filter( 'get_terms' , 'filter_cptonomies_get_terms' , 20 , 3 );

    it shows the LANG when in the back end, but when in the front end, it shows only the ones in the current languages.

  6. sireneweb
    Member
    Posted 2 years ago #

    there are a bug with your first function ffto, it doesn't display the good langage correctly. I suspect get_language_for_element

  7. ffto
    Member
    Posted 2 years ago #

    Hi sireneweb,

    does the second function I posted worked? I compared your function with the WPML one "get_language_for_element" and it's exactly the same code.

    i've updated the function again, to validate if the user has WPML turned off, so it degrades gracefully without errors.

    function filter_cptonomies_get_terms ( $terms, $taxonomies, $args ){
    	global $sitepress;
    	if ( !isset( $sitepress ) ) return $terms;
    
    	$post_types = get_post_types();
    	foreach ( $terms as $iTerm => $term ){
    		if ( in_array( $term->taxonomy , $post_types ) ){
    			$post		= get_posts( 'suppress_filters=1&post_per_page=1&post_type='.$term->taxonomy.'&name='.$term->slug );
    			$lang		= strtoupper( $sitepress->get_language_for_element( $post[0]->ID, "post_{$term->taxonomy}" ) );
    			$current	= strtoupper( ICL_LANGUAGE_CODE );
    
    			if ( is_admin() ){
    				$term->name = "{$term->name} ({$lang})";
    			}elseif ( $lang !== $current ) {
    				unset( $terms[$iTerm] );
    			}
    		}
    	}
    	return $terms;
    }
    add_filter( 'get_terms' , 'filter_cptonomies_get_terms' , 20 , 3 );

    Do you have an example of the bug? I'm not able to reproduce the problem.

  8. sireneweb
    Member
    Posted 2 years ago #

    For me, the bug comes from your request, maybe you can try to add for each language the title + lang to see if it display the sames result than your function with ({$lang})

    for exemple :
    my item 1 FR
    my item 2 EN

    check my request :
    return $wpdb->get_var("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_id='{$element_id}' AND element_type='{$el_type}'");

    Then after you don't get the sames result with your function

  9. sireneweb
    Member
    Posted 2 years ago #

    function filter_cptonomies_get_terms ( $terms, $taxonomies, $args ){
    	global $sitepress;
    	$post_types = get_post_types();
    	foreach ( $terms as $iTerm => $term ){
    		if ( in_array( $term->taxonomy , $post_types ) ){
    			$post		= get_posts( 'post_per_page=1&post_type='.$term->taxonomy.'&name='.$term->slug );
    			//$lang		= strtoupper( get_language_for_element_w($term->term_id, 'post_pictos_produit') );
    			$lang		= strtoupper( $sitepress->get_language_for_element($term->term_id, "post_{$term->taxonomy}" ) );
    			$current	= strtoupper( ICL_LANGUAGE_CODE );
    
    			if ((is_admin())&&($lang == $current)){
    				$term->name = "{$term->name} ({$lang})";
    			}elseif ( $lang !== $current ) {
    				unset( $terms[$iTerm] );
    			}
    		}
    	}
    	return $terms;
    }

    i solve the bug, now we display only the terms from the current language and good terms associated from the language.

  10. NTD
    Member
    Posted 1 year ago #

    Hi,

    This is exactly what I'd need, but unfortunately your function does not seem to work for me.

    Any ideas?

  11. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    Sorry, NTD. I, unfortunately, have no experience with WPML whatsoever although it seems like I should check it out.

    Since this post has been pretty active, I was hoping someone would help you out, and maybe they'll speak up soon.

    When I get some free time, I'll start to do some research on WPML. Hope you get some help soon!

  12. NTD
    Member
    Posted 1 year ago #

    Hey Rachel,

    No worries, I'll look into it when I have the time and post updates if I can find some resolution.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic