Update the sorts when changing the site default language

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPrint this page

To allow sorting for different languages through custom plugins like WPML the plugin implemented a custom functionality which also save the sort language. The feature becomes active right after plugin activation, even if there’s no multilingual code active yet to preserve sorts compatibility for later usage.

As default the Advanced Post Types Order plugin use the site default language when saving the language attribute. If for some reason, the site language is being changed, apparently the orders does not apply anymore. This is an expected result, since the sorts where created for a particular language, which now has changed. There is a workaround for such scenario:

  1. Re-create the ordering, they will be save using the new language attribute and apply accordingly.
  2. Update the old language to new one within the sorts.

To update the language attribute within the sorts, the following code can be used. This code should be placed within a file and saved within WordPress root. Or if copied over into different directory, the include(‘wp-config.php’) line should be adjusted accordingly.

The _APTO_OLD_LANGUAGE_CODE constant should include the old language code. The _APTO_NEW_LANGUAGE_CODE need to held the new one.

    //define the constants
    define ('_APTO_OLD_LANGUAGE_CODE', 'en');
    define ('_APTO_NEW_LANGUAGE_CODE', 'de');
    
    include('wp-config.php');
    
    global $post;
    
    $args   =   array(
                        'post_type'         =>  'apto_sort',
                        'post_parent'       =>  '0',
                        'posts_per_page'    =>  -1
                        );
    $custm_query        =   new WP_Query( $args );
    while( $custm_query->have_posts() )
        {
            $custm_query->the_post();   
            
            //get all sort views
            $args   =   array(
                                'post_type'         =>  'apto_sort',
                                'post_parent'       =>  $post->ID,
                                'posts_per_page'    =>  -1  
                                );
            $sort_views =   get_posts( $args );
            if ( count ( $sort_views ) < 1 ) 
                continue;
                
            foreach ( $sort_views   as  $sort_view) 
                {
                    $_view_language =   get_post_meta( $sort_view->ID, '_view_language', TRUE );
                    if ( $_view_language !=  _APTO_OLD_LANGUAGE_CODE )
                        continue;
                        
                    update_post_meta( $sort_view->ID, '_view_language', _APTO_NEW_LANGUAGE_CODE );
                    
                }
            
        }