wordpressの多言語化プラグインの「qTranslate x」を使っていて特定の言語ごとに記事を出し分ける必要がありました。
具体的に言うと
日本語のみ入力のある記事では言語が日本の時のみ表示し、それ以外の言語では非表示としたい
この場合、一番簡単な方法は設定>言語>URLモード内の「 デフォルト言語の URL 言語情報を隠す」にチェックを入れます。
この設定はタイトルの入力有無で判断しているため、非表示したい言語はタイトルを未入力にする必要があります。
ただ、タイトルではなく本文の入力有無で判断する必要なケースもあると思います。
その場合、function.phpに以下を記述することで対応可能です
remove_filter('posts_where_request', 'qtranxf_excludeUntranslatedPosts',10,2);
add_filter('posts_where_request', 'qcstm',10,2);
function qcstm($where,&$query) {//WP_Query
//qtranxf_dbg_echo('qtranxf_excludeUntranslatedPosts: post_type: ',$query->query_vars['post_type']);
switch($query->query_vars['post_type']){
//known not to filter
case 'nav_menu_item':
return $where;
//known to filter
case '':
case 'any':
case 'page':
case 'post':
default: break;
}
//qtranxf_dbg_echo('qtranxf_excludeUntranslatedPosts: post_type is empty: $query: ',$query, true);
//qtranxf_dbg_echo('qtranxf_excludeUntranslatedPosts: $where: ',$where);
//qtranxf_dbg_echo('qtranxf_excludeUntranslatedPosts: is_singular(): ',is_singular());
$single_post_query=$query->is_singular();//since 3.1 instead of top is_singular()
if($single_post_query){
$single_post_query = preg_match('/ID\s*=\s*[\'"]*(\d+)[\'"]*/i',$where,$matches)==1;
//qtranxf_dbg_echo('qtranxf_excludeUntranslatedPosts: $single_post_query: ',$single_post_query);
//if($single_post_query){
// //qtranxf_dbg_echo('qtranxf_excludeUntranslatedPosts: $matches[1]:',$matches[1]);
//}
}
if(!$single_post_query){
global $wpdb;
$lang = qtranxf_getLanguage();
$where .= ' AND '.qtranxf_where_clause_translated_posts_for_he($lang,$wpdb->posts);
}
return $where;
}
function qtranxf_where_clause_translated_posts_for_he($lang,$table_posts) {
$post_content = $table_posts.'.post_title';
return "($post_content='' OR $post_content LIKE '%![:$lang!]%' ESCAPE '!' OR $post_content LIKE '%%' OR ($post_content NOT LIKE '%![:!]%' ESCAPE '!' AND $post_content NOT LIKE '%%'))";
}
