0

I'm trying to filter custom taxonomy by language. To achieve this, I use WP_Query class with the following arguments:

                $args = array(
                    'post_type'      => ['post', 'page'],
                    'post_status'    => 'publish',
                    'lang'    => $language,
                    'tax_query' => array(
                        array(
                            'taxonomy' => $taxonomy_custom_category,
                            'field'    => 'name',
                            'terms'    => $taxonomy_term,
                        ),
                    ),
                );

Where $language is one of the language retrieved by pll_languages_list().

However, except for the language by default, the query results is always empty. Is there any way to build a form with Polylang filtered by language that returns the correct results for a custom taxonomy?

1 Answer 1

0

Nevermind, I finally found. This is not documented, but going through Polylang code I found a way.

First retrieve the languages with their taxonomy id with

$lang = get_terms('term_language', ['hide_empty' => false])

which gives an object that can be called as $lang->name and $lang->term_id.

Then, the wp_query should look like this:

                $args = array(
                    'post_type'      => ['post', 'page'],
                    'post_status'    => 'publish',
                    'numberposts' => -1,
                    'nopaging'    => true,
                    'tax_query' => array(
                        'relation' => 'AND',
                        array(
                            'taxonomy' => $taxonomy_custom_category,
                            'field'    => 'name',
                            'terms'    => $taxonomy_term,
                        ),
                        array(
                            'taxonomy' => 'language',
                            'field'    => 'term_taxonomy_id',
                            'terms'    => $lang->term_id,
                        ),
                    ),
                );

From there, it's easy to build a form or whatever you want your plugin to do.

I know this is a bit off topic, but this information is unavailable anywhere. I nevertheless found that many plugin developers are getting stuck as I was.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.