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.
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:
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 );
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();
}
?>
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
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.