Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client is working on a site under development and trying to issue posts in a specific language using WPML. They used custom code to query posts, but it did not work for switching the language.
Solution:
We recommended using a different approach since we cannot debug custom-coded templates. Here is a working example that the client can adapt:

<?php    
// Get the current frontend language
$current_lang = apply_filters( 'wpml_current_language', NULL );
 
$args = array(
    'post_type'      => 'post',
    'posts_per_page' => -1,
'suppress_filters' => false,
    'lang'           => $current_lang, // Filter posts by the current language
);
 
$posts = get_posts($args);
 
if ($posts) {
    foreach ($posts as $post) {
        setup_postdata($post);
        // Display the post data
        echo '<h2>' . get_the_title() . '</h2>';
        echo '<div>' . get_the_excerpt() . '</div>';
    }
    wp_reset_postdata();
}
 
?>

If this solution does not apply because it is outdated or not suitable for your case, we highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If further assistance is needed, please open a new support ticket at WPML support forum.

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.

No supporters are available to work today on this forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by Dražen Duvnjak 2 weeks, 1 day ago.

Assisted by: Dražen Duvnjak.

Author Posts
September 6, 2024 at 9:24 am #16147898

yuryB-5

Background of the issue:
I am working on a site under development and trying to issue posts in a certain language using WPML. I received a suggestion to use the following code:

'blog',
'post_status' => 'publish',
'numberposts' => 1,
'order' => 'DESC',
);
$first_blog_post = get_posts($first_blog_post_args);
$all_blog_posts_args = array(
'post_type' => 'blog',
'post_status' => 'publish',
'post__not_in' => array($first_blog_post[0]->ID),
'posts_per_page' => -1,
'order' => 'DESC',
);
$all_blog_posts = get_posts($all_blog_posts_args);
?>

Symptoms:
The proposed code to switch the language and query posts does not work.

Questions:
Why doesn't the provided code work for switching the language and querying posts?
Is there a different approach to achieve the same result?

September 6, 2024 at 9:48 am #16148001

Dražen Duvnjak
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hello,

You have been inactive for a while, so I have created a ticket for you.

Please try to reproduce the issue, and let me know when done how I can check and what is wrong, so I can check further.

Thanks,
Drazen

September 6, 2024 at 9:53 am #16148075

yuryB-5

The site (which you provided) takes a very long time to load. The basic idea is to take this template
<?php

/*
* Template Name: Main Blog
* Template Post Type: page
*/
?>

<?php get_header(); ?>
<?php $page_id = $post->ID; ?>
<?php
// Get the current language
$current_lang = apply_filters( 'wpml_current_language', NULL );

// Temporarily switch to the current frontend language
do_action( 'wpml_switch_language', $current_lang );
$first_blog_post_args = array(
'post_type' => 'blog',
'post_status' => 'publish',
'numberposts' => 1,
'order' => 'DESC',
);
$first_blog_post = get_posts($first_blog_post_args);
$all_blog_posts_args = array(
'post_type' => 'blog',
'post_status' => 'publish',
'post__not_in' => array($first_blog_post[0]->ID),
'posts_per_page' => -1,
'order' => 'DESC',
);
$all_blog_posts = get_posts($all_blog_posts_args);
?>

<div class="blog <?php echo $first_blog_post[0]->ID ?>">
<h1 class="blog__headline" animation animation-arrow="bottom"><?php echo get_the_title() ?></h1>

ID ); ?>"
animation animation-arrow="bottom">



ID, 'crb_change_locale_fr')))) {
switch_to_locale('fr_FR');
$date=get_the_date('j F Y', $first_blog_post[0]->ID);
$date = mb_convert_case($date, MB_CASE_TITLE, 'UTF-8');;
echo $date;
}
else echo get_the_date('j F Y', $first_blog_post[0]->ID);
// Верните локаль обратно
if ((function_exists('restore_previous_locale') && (carbon_get_post_meta($first_blog_post[0]->ID, 'crb_change_locale_fr')))) {
restore_previous_locale();
} ?>

ID, 'crb_blog_anonce_title'); ?>
ID, 'crb_blog_anonce_subtitle'); ?>

<div class="blog__articles">
<?php foreach ($all_blog_posts as $item) {
?>
ID); ?>"
animation animation-arrow="bottom">







ID, 'crb_blog_anonce_title'); ?>
ID, 'crb_blog_anonce_subtitle'); ?>


<?php } ?>
</div>
</div>

<?php get_footer(); ?>

Carbon fields can be omitted

September 6, 2024 at 10:05 am #16148119

Dražen Duvnjak
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hello Yury,

I understand but I can not debug your custom-coded template, that is not part of our support. I can provide a working example and you can then adapt that to your needs, or if you are not sure how, then you need to hire WPML contractors.

For example on the test site you can see our hooks working, post is shown in the header, please check it out and try to adjust it to your needs.

Link:
- hidden link

Code used:

<?php	
// Get the current frontend language
$current_lang = apply_filters( 'wpml_current_language', NULL );

$args = array(
    'post_type'      => 'post',
    'posts_per_page' => -1,
'suppress_filters' => false,
    'lang'           => $current_lang, // Filter posts by the current language
);

$posts = get_posts($args);

if ($posts) {
    foreach ($posts as $post) {
        setup_postdata($post);
        // Display the post data
        echo '<h2>' . get_the_title() . '</h2>';
        echo '<div>' . get_the_excerpt() . '</div>';
    }
    wp_reset_postdata();
}

?>
September 6, 2024 at 11:03 am #16148604

yuryB-5

Initially, you confused me by saying that lang in the request parameter will not give anything. But now with adding both lang and 'suppress_filters' => false, everything works

September 6, 2024 at 11:07 am #16148613

Dražen Duvnjak
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hello,

You are right, I have shared not completely correct information the first time, my apologies.

Glad to hear it works fine now.

Regards,
Drazen

yuryB-5 confirmed that the issue was resolved on 2024-09-06 11:03:17.
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.