Mixed language post display
-
Hey Chouby,
I’ve been trying to figure out how to display posts of different languages on a single page to no avail so far. My site only uses two languages: en, de. I’m using a custom page template where I query the database for posts with a custom post type and a custom field. The following query yields the result you’d expect for the current language:
$featured_query = new WP_Query(array( 'post_type' => 'project', 'meta_key' => 'featured_project', 'meta_value' => 'featured', 'meta_compare' => 'LIKE' ));Once I’m adding the ‘lang’ parameter to this query it either does not return any result – for the values ‘de’ and ‘any’ – or just the default results – for the values ”, ‘en’ and ‘en,de’. Trying to add the languages before or after Polylang processed the query via a filter hook yields the same results.
I then tried altering the ‘tax_query’ parameter for the language – which is only set when the ‘lang’ parameter is not set in the query.
add_filter('pre_get_posts', 'set_languages_for_project_posts', 30); // after polylang function set_languages_for_project_posts($query) { if (!is_admin() && function_exists('pll_default_language')) { $qv = $query->query_vars; if (isset($qv['post_type']) && $qv['post_type'] == 'project') { $lang_terms = get_terms('language'); $term_ids = array(); foreach ($lang_terms as $lang_term) { $term_ids[] = $lang_term->term_id; } $query->set('tax_query', array( array( 'field' => 'term_taxonomy_id', 'operator' => 'IN', 'taxonomy' => 'language', 'terms' => $term_ids ) )); } } return $query; }But this still renders just the default posts. The $term_ids array will contain two values – 10 for en and 13 for de. Changing ‘terms’ to just 13 renders no posts at all. Resetting it to 10 shows also the default posts again.
Since both approaches yield similar results I feel like I’m starting at the wrong end to achieve my goal. Can you point me into the right direction?
Thanks.
DavidResources considered:
The topic ‘Mixed language post display’ is closed to new replies.