Skip to content

Instantly share code, notes, and snippets.

@TheHeat
Last active February 26, 2016 19:39
Show Gist options
  • Save TheHeat/e4a6973000e16fb63239 to your computer and use it in GitHub Desktop.
Save TheHeat/e4a6973000e16fb63239 to your computer and use it in GitHub Desktop.
Using qtranslate-x exclude untranslated posts from blog feed and cat archives
// lets us know if the current post is available in the current locale
function proper_is_translated($id = 0){
global $post;
if($id == 0){
$context = $post->ID;
}else{
$context = $id;
}
// On the site we use en_GB & cy_GB locales
// qtranxf_isAvailableIn() looks for en & cy (the versions we use in the URL structure)
$locale = get_locale();
$locale_trimmed = substr($locale, 0, 2);
$translated = qtranxf_isAvailableIn($context, $locale_trimmed);
return $translated;
}
// list all untranslated post ids of a given type
function proper_list_untranslated($post_type = null){
// Set post type
if($post_type){
$type = $post_type;
}
elseif(is_home()){
$type = 'post';
}
else{
$query = get_queried_object();
$type = $query->post_type;
}
$all_of_type = get_posts(array(
'post_type' => $type,
'posts_per_page' => -1
)
);
$untranslated = array();
foreach ($all_of_type as $one_of_type) {
$one_id = $one_of_type->ID;
if(!proper_is_translated($one_id)){
$untranslated[] = $one_id;
}
}
return $untranslated;
}
// Tweak the main query to exclude untranslated posts
function proper_tweak_query( $query ) {
// exclude untranslated posts from the blog feed
if ( !is_admin() && $query->is_main_query() && $query->is_home() ) {
$untranslated = proper_list_untranslated();
$query->set('post__not_in', $untranslated );
}elseif ( !is_admin() && $query->is_main_query() && $query->is_category() ) {
$untranslated = proper_list_untranslated();
$query->set('post__not_in', $untranslated );
}
}
// Load our function when hook is set
add_action( 'pre_get_posts', 'proper_tweak_query' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment