Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 3 replies, has 2 voices.

Last updated by adrienR-5 5 months, 3 weeks ago.

Assisted by: Mateus Getulio.

Author Posts
August 14, 2024 at 12:08 pm #16070320

adrienR-5

<b>Background of the issue: </b>
I'm trying to fetch the number of posts from a specific taxonomy.

This is my current function code:

[...]
$args = array(
    'post_type' => 'actualite-franchise',
    'fields' => 'ids',
    'posts_per_page' => 1,
    'no_found_rows' => false,
    'post_status' => 'publish',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'type-actualite',
            'field' => 'slug',
            'terms' => 'short-news'
        ),
        array(
            'taxonomy' => 'franchise_category',
            'field' => 'term_id',
            'terms' => $current_terms, /* $current_terms = array(953) in this case */
            'operator' => 'IN',
        )
    ),
    'ignore_sticky_posts' => 1,
    'suppress_filters' => false,
);
$the_query = new WP_Query($args);
return $the_query->found_posts;
}

<b>Symptoms: </b>
The issue is that it is returning me the total number of those posts from this custom taxonomy but for my default language.

<b>Questions: </b>
How can I get the total number of posts of 'nl-be' (Dutch Belgium) for example if I visit the page in WPML current language nl-be?

August 14, 2024 at 12:15 pm #16070409

adrienR-5

It seems that the documentation is wrong, we need to set suppress_filters to true in order to fetch the posts within the current language.

This helped me: https://wordpress.stackexchange.com/a/406763

August 14, 2024 at 7:10 pm #16072311

Mateus Getulio
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello Adrien,

I'm glad to know that the issue seems fixed now, thank you for sharing the solution.

I checked it and there were other times where we recommended setting suppress_filters to true in order to avoid other filters from changing the result of the wp query:

https://wpml.org/forums/topic/how-to-make-wordpress-search-posts-and-pages-in-all-language/
https://wpml.org/forums/topic/search-results-from-other-languages-not-displaying/

Can you point me to the documentation that needs to be edited? We'll check it and make the needed changes.

Thank you,
Mateus

August 14, 2024 at 7:28 pm #16072340

adrienR-5

Hello Mateus,

Hope you're doing well

Here's the documentation that would need to be edited I think: https://wpml.org/documentation/support/debugging-theme-compatibility/#issue-wp_queryargs-or-get_postsargs-doesnt-filter-out-correct-posts-ids-for-the-current-language

The topic ‘[Closed] Fetch with WP_Query or get_posts only the posts from a specific taxonomy (for current language)’ is closed to new replies.