Like everyone else I′ve been looking for a solution and thanks to the community I think I found a limited fix. My requirement was to allow a recent posts widget to show up default language posts for other language (posts not translated, selectable by category).
In this other thread they point at syllogic blog where there is an example of a function to override the current language. It makes use of pre_get_posts to modify the main query but as I needed this to happen on a very specific query, this other thread in stackexchange give me the idea on how to implement it.
I included it in functions.php modified like that:
// PLUGIN TW Recent Posts Widget + OVERRIDE Polylang default language
// tell WordPress about our new query var
function wpse52480_query_vars( $query_vars ){
$query_vars[] = 'default_lang_query';
return $query_vars;
}
add_filter( 'query_vars', 'wpse52480_query_vars' );
// check if our query var is set in any query
function wpse52480_pre_get_posts( $query ){
if( !is_admin() && isset( $query->query_vars['default_lang_query'] ) ) {
if ( function_exists('pll_default_language') ){
$terms = get_terms('post_translations'); //polylang stores translated post IDs in a serialized array in the description field of this custom taxonomy
$defLang = pll_default_language(); //default lanuage of the blog
$curLang = pll_current_language(); //current selected language requested on the broswer
$filterPostIDs = array();
foreach($terms as $translation){
$transPost = unserialize($translation->description);
//if the current language is not the default, lets pick up the default language post
if($defLang!=$curLang) $filterPostIDs[]=$transPost[$defLang];
}
if($defLang!=$curLang){
$query->set('lang' , $defLang.','.$curLang); //select both default and current language post
$query->set('post__not_in', $filterPostIDs); // remove the duplicate post in the default language
}
}
}
return $query;
}
add_action( 'pre_get_posts', 'wpse52480_pre_get_posts' );
Then I made a modification in the code of the plugin TW Recent Posts (it′s simple and handy) to modify wp_query. At line 154 in tw-recent-posts-widget.php change it like:
$args = array(
'default_lang_query' => true,
'cat' => $category,
'posts_per_page' => $count,
'orderby' => $orderby,
'order' => $order,
'nopagging' => true
);
$wp_query= new WP_Query( $args );
This now allows this widget to pick up posts in the default language even if we are on a translated one. The function can be used in php templates to extend its use.