Wordpress - Change page template programmatically ?

Finally found it! If I understand your question right, the template is basically saved as metadata that needs to be updated.

update_post_meta( $post_id, '_wp_page_template', 'your_custom_template' );
// or
update_metadata('post_type',  $post_id, '_wp_page_template', 'your_custom_template' );

Source and further info


The best (canonical) way is use template_include hook: http://codex.wordpress.org/Plugin_API/Filter_Reference/template_include

Example code:

function language_redirect($template) {
    global $q_config;
    $new_template = locate_template( array( 'page-'.$q_config['lang'].'.php' ) );
    if ( '' != $new_template ) {
        return $new_template ;
    }
    return $template;
}
add_action( 'template_include', 'language_redirect' );

Should be possible using the template_include hook. Code is untested:

 add_action( 'template_include', 'language_redirect' );

 function language_redirect( $template ) {
      global $q_config;
      $lang = ( 'en' === $q_config['lang'] ) ? 'en' : 'de';

      $template = str_replace( '.php', '_'.$lang.'.php', $template );
      return $template;
 }