Custom loop in controller and WPML "append" translated posts

Hello
after starting to create a new theme using Sage9, I’ve discovered that there could be issues using WPML multilanguage plugin.

I’ve got the following custom loop in my FrontPage controller, created following Sage book and other Discourse posts…

public static function latestItemsLoop()
{
    $args = array(
    'post_type' => 'items',
    'posts_per_page' => 6,
    );
    $index_items = get_posts($args);

    return array_map(function ($post) {
        return [
            'itemTitle' => apply_filters('get_the_title', $post->post_title),
            [...etc...]
        ];
    }, $index_items );
}

This works as expected: using a @foreach(FrontPage::latestItemsLoop() as $indexItem)” it shows the latest 6 post from a CPT with all the data from the array_map, and that’s fine.

Now, if in the WP Admin I add a translation of one of those posts, this new translation is added to this foreach cycle, no matter the language I select i.e. displayed in the browser.

What I’m missing? Should I have to use a “global $sitepress;” sonmewhere (in the controller?) and check the ICL_LANGUAGE_CODE maybe? And how to do that?..

Confused…

Hey @Saxpaolo - this is more of a WPML support question, but I happen to be working with WPML at the moment. Try adding 'suppress_filters' => false to your query args:

public static function latestItemsLoop()
{
    $args = array(
    'post_type' => 'items',
    'posts_per_page' => 6,
    'suppress_filters' => false, // add this
    );
    $index_items = get_posts($args);

    return array_map(function ($post) {
        return [
            'itemTitle' => apply_filters('get_the_title', $post->post_title),
            [...etc...]
        ];
    }, $index_items );
}
1 Like

This topic was automatically closed after 42 days. New replies are no longer allowed.