function the_posts($posts) { global $wpml_post_translations; $ids = array(); foreach ($posts as $single_post) { $ids[] = $single_post->ID; } $wpml_post_translations->prefetch_ids($ids); if (!is_admin() && $this->get_setting('show_untranslated_blog_posts') && $this->get_current_language() != $this->get_default_language()) { // show untranslated posts global $wp_query; $default_language = $this->get_default_language(); $current_language = $this->get_current_language(); $debug_backtrace = $this->get_backtrace(4, true); //Limit to first 4 stack frames, since 3 is the highest index we use /** @var $custom_wp_query WP_Query */ $custom_wp_query = isset($debug_backtrace[3]['object']) ? $debug_backtrace[3]['object'] : false; //exceptions if ($current_language == $default_language || $wp_query != $custom_wp_query || !$custom_wp_query->is_posts_page && !$custom_wp_query->is_home || $wp_query->is_singular || !empty($custom_wp_query->query_vars['category__not_in']) || !empty($custom_wp_query->query_vars['tag__not_in']) || !empty($custom_wp_query->query_vars['post__in']) || !empty($custom_wp_query->query_vars['post__not_in']) || !empty($custom_wp_query->query_vars['post_parent'])) { return $posts; } // get the posts in the default language instead $this_lang = $this->this_lang; $this->this_lang = $default_language; remove_filter('the_posts', array($this, 'the_posts')); $custom_wp_query->query_vars['suppress_filters'] = 0; if (isset($custom_wp_query->query_vars['pagename']) && !empty($custom_wp_query->query_vars['pagename'])) { if (isset($custom_wp_query->queried_object_id) && !empty($custom_wp_query->queried_object_id)) { $page_id = $custom_wp_query->queried_object_id; } else { // urlencode added for languages that have urlencoded post_name field value $custom_wp_query->query_vars['pagename'] = urlencode($custom_wp_query->query_vars['pagename']); $page_id = $this->wpdb->get_var($this->wpdb->prepare("SELECT ID FROM {$this->wpdb->posts}\n WHERE post_name = %s\n AND post_type='page'", $custom_wp_query->query_vars['pagename'])); } if ($page_id) { $tr_page_id = icl_object_id($page_id, 'page', false, $default_language); if ($tr_page_id) { $custom_wp_query->query_vars['pagename'] = $this->wpdb->get_var($this->wpdb->prepare("SELECT post_name\n FROM {$this->wpdb->posts}\n WHERE ID = %d ", $tr_page_id)); } } } // look for posts without translations if ($posts) { $pids = false; foreach ($posts as $p) { $pids[] = $p->ID; } if ($pids) { $trids = $this->wpdb->get_col($this->wpdb->prepare("\n\t\t\t\t\t\tSELECT trid\n\t\t\t\t\t\tFROM {$this->wpdb->prefix}icl_translations\n\t\t\t\t\t\tWHERE element_type='post_post'\n\t\t\t\t\t\t AND element_id IN (" . wpml_prepare_in($pids, '%d') . ")\n\t\t\t\t\t\t AND language_code = %s", $this_lang)); if (!empty($trids)) { $posts_not_translated = $this->wpdb->get_col("\n\t\t\t\t\t\t\tSELECT element_id, COUNT(language_code) AS c\n\t\t\t\t\t\t\tFROM {$this->wpdb->prefix}icl_translations\n\t\t\t\t\t\t\tWHERE trid IN (" . join(',', $trids) . ") GROUP BY trid HAVING c = 1\n\t\t\t\t\t\t"); if (!empty($posts_not_translated)) { $GLOBALS['__icl_the_posts_posts_not_translated'] = $posts_not_translated; add_filter('posts_where', array($this, '_posts_untranslated_extra_posts_where'), 99); } } } } //fix page for posts unset($custom_wp_query->query_vars['pagename']); unset($custom_wp_query->query_vars['page_id']); unset($custom_wp_query->query_vars['p']); $my_query = new WP_Query($custom_wp_query->query_vars); add_filter('the_posts', array($this, 'the_posts')); $this->this_lang = $this_lang; // create a map of the translated posts foreach ($posts as $post) { $trans_posts[$post->ID] = $post; } // loop original posts foreach ($my_query->posts as $k => $post) { // loop posts in the default language $trid = $this->get_element_trid($post->ID); $translations = $this->get_element_translations($trid); // get translations if (isset($translations[$current_language])) { // if there is a translation in the current language if (isset($trans_posts[$translations[$current_language]->element_id])) { //check the map of translated posts $my_query->posts[$k] = $trans_posts[$translations[$current_language]->element_id]; } else { // check if the translated post exists in the database still $_post = $this->wpdb->get_row($this->wpdb->prepare("SELECT * FROM {$this->wpdb->posts} WHERE ID = %d AND post_status='publish' LIMIT 1", $translations[$current_language]->element_id)); if (!empty($_post)) { $_post = sanitize_post($_post); $my_query->posts[$k] = $_post; } else { $my_query->posts[$k]->original_language = true; } } } else { $my_query->posts[$k]->original_language = true; } } if ($custom_wp_query == $wp_query) { $wp_query->max_num_pages = $my_query->max_num_pages; } $merged_posts = array_merge($my_query->posts, $posts); $unique_posts = $this->wp_api->array_unique($merged_posts, SORT_REGULAR); $posts = array_values($unique_posts); unset($GLOBALS['__icl_the_posts_posts_not_translated']); remove_filter('posts_where', array($this, '_posts_untranslated_extra_posts_where'), 99); } return $posts; }