Skip Navigation

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

Problem:
The client was experiencing an issue where posts were not displaying unless one post was translated, even though the WPML settings were set to 'Translatable - use translation if available or fallback to default language'. The posts were sorted by a custom taxonomy and needed to be visible in the Spanish translation without being translated, as they linked to primarily English content.

Solution:
We resolved the issue by adding the parameter

'hide_empty' => false

to the

get_terms()

function in the custom WP_Query. This allowed terms with no posts translated into the secondary language to be displayed.

$terms = get_terms(array(
    'taxonomy' => $taxonomy_slug,
    'child_of' => $parent_term_id, // Specify the parent term ID
    'hide_empty' => false
));

If you're experiencing a similar issue, we recommend trying this solution. However, please note that this solution might be irrelevant if it's outdated or not applicable to your case. We highly recommend checking related known issues, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If the problem persists, please open a new support ticket with us.

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.

Tagged: ,

This topic contains 16 replies, has 2 voices.

Last updated by Long Nguyen 8 months, 3 weeks ago.

Assisted by: Long Nguyen.

Author Posts
February 29, 2024 at 2:39 pm

kurtK-7

Posts not showing up unless 1 is converted even though the setting is set to default

February 29, 2024 at 3:52 pm
February 29, 2024 at 8:50 pm #15360628

kurtK-7

I was asked to create a stripped down version of the site to allow your support and development team to have an area to against. I have loaded some sample data for testing. The Language switcher is in the footer. I was not able to install the WPML ACF plugin either.

hidden link
hidden link

Please let me know what you discover.

Happy to provide greater details but all of this should exist in my chat log that this ticket was created from.

March 1, 2024 at 8:56 am #15361503

Long Nguyen
Supporter

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi Kurt,

Thank you for contacting WPML support, I’m happy to help you with this issue.

I'm trying to understand your issue and replicate the issue on a new installation of WordPress. But somehow I see it works properly. The steps I've done:

1. Create a Resource CPT and a Resource Category taxonomy.
2. Set the preference to fallback "Translatable - use translation if available or fallback to default language".
3. Create a category and resource post, not translate them.
4. Create a page template with the following code, to get the post from the child category:

$taxonomy_slug = 'resource-category';
$parent_term_id = 9; /* Product Resources ID in Resources Center Categories Taxonomy */

$terms = get_terms(array(
    'taxonomy' => $taxonomy_slug,
    'child_of' => $parent_term_id, // Specify the parent term ID
    'fields' => 'slugs', // Fetch slugs only
));

// Loop through each child term
foreach ($terms as $term) {
    // Set up the query arguments
    $args1 = array(
        'post_type'         => 'resource',
        'order'             => 'ASC',
        'posts_per_page'    => -1,
        'tax_query'         => array(
            array(
                'taxonomy'  => $taxonomy_slug,
                'field'     => 'slug',
                'terms'     => $term,
            ),
        ),
    );

    // Perform the query
    $the_query = new WP_Query($args1);

    // Check if there are any posts for this term
    if ($the_query->have_posts()) {
        echo '<ul>';
		while ( $the_query->have_posts() ) {
			$the_query->the_post();
			echo '<li>' . esc_html( get_the_title() ) . '</li>';
		}
		echo '</ul>';
    }
    wp_reset_postdata();
}

5. Create a page, set the page template and translate it.
6. Visit the frontend, I can see the resource post in both language English and French.

Here is the new sandbox site URL hidden link
You can see the code in the file page-resource.php under the theme folder.

Note: you shouldn't translate the category, if you translate the category, there isn't a post that is associated with the translated category and it doesn't display in the frontend.

Let me know if I understand your issue correctly.

March 3, 2024 at 10:49 pm #15367341

kurtK-7

Thank you for looking into this further.
Let me break this down for you so you have the same information the chat support person had.

There is a CPT - Resource Center. That CPT has two Taxonomies associated to it. One of them is Resource Center Categories and the other is Resource Center Partners.

Regarding the CPT Resource Center in the WPML settings for Post Types Translation, I have that set to "Translatable - use translation if available or fallback to default language"

I need these posts visible in my Spanish translation but have not future plans to translate them as they link to primarily English PDF and Video links.

These posts are sorted by the custom taxonomy. The terms for the 'Resource Center Categories' are displayed as titles in an accordion interface. I would like these terms to display translated in the interface. When you arrive on the page and you are viewing the Spanish translation site, these terms (accordion section titles) should be in Spanish.

For this custom taxonomy - Resource Center Categories, the WPML settings for Taxonomies Translation is set to "Translatable - use translation if available or fallback to default language."

This setting is correct. I am translating these terms, and the posts assigned to these terms should display regardless of translation status.

The last taxonomy, Resource Center Partners, further filters the posts to group them by company. In WPML settings for Taxonomy I also have this set to "Translatable - use translation if available or fallback to default language." As these are company names I don't imagine I would translate them but I need these terms to display regardless of the language they are being viewed in.

I recognize this is custom coding with few different factors involved so I stripped it down to s simple WP_Query and ForEach loop to display the posts. I get zero results in Spanish. I have tried with the taxonomies converted into spanish as well as cleared out.

// Define the parent term ID or slug
$taxonomy_slug = 'resource_center_categories';
//$parent_term_id = apply_filters( 'wpml_object_id', 232, $taxonomy_slug, TRUE );
$parent_term_id = 232;

// Get child terms of the parent term
$terms = get_terms(array(
'taxonomy' => $taxonomy_slug,
'child_of' => $parent_term_id, // Specify the parent term ID
));

// Loop through each child term
foreach ($terms as $term) {
// Define your query arguments for each category
$args = array(
'post_type' => 'resource-center', // You can specify your custom post type if needed
'posts_per_page' => -1, // -1 to fetch all posts, you can set a limit if needed
'tax_query' => array(
array(
'taxonomy' => $taxonomy_slug,
'field' => 'term_id',
'terms' => $term->term_id, // Use the current child term ID
),
),
);

// Instantiate a new WP_Query object for each term
$query = new WP_Query($args);

// Check if there are posts available for the term
if ($query->have_posts()) :
// Get the term object
echo '<h2>' . $term->name . '</h2>'; // Display term name

// Start the loop for the term
while ($query->have_posts()) : $query->the_post();
// Your loop content here
the_title('<h3>', '</h3>');
the_content();
endwhile;

// Restore original post data
wp_reset_postdata();
endif;
}

March 3, 2024 at 11:02 pm #15367345

kurtK-7

The more I test, the more I think the issue happens in the ForEach Loop.

When I just have a WP Query to retrieve the results (without the added Foreach Loop to group the results then I get results.

English - With Query and ForEach - Results for both
hidden link

Spanish - With Query and ForEach - Results for only the second
hidden link

<?php
$args2 = array(
'post_type' => 'resource-center', // You can specify your custom post type if needed
'posts_per_page' => -1, // -1 to fetch all posts, you can set a limit if needed
);

// Instantiate a new WP_Query object for each term
$query = new WP_Query($args2);

// Check if there are posts available for the term
if ($query->have_posts()) :
echo '<ol>';
// Start the loop for the term
while ($query->have_posts()) : $query->the_post();
// Your loop content here
the_title('

  • ', '
  • ');
    endwhile;

    // Restore original post data

    echo '</ol>';
    wp_reset_postdata();

    endif;
    ?>

    March 4, 2024 at 3:19 am #15367451

    Long Nguyen
    Supporter

    Languages: English (English )

    Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

    Hi,

    As I already mentioned above, if you don't translate the term, the fallback posts are displayed correctly in the frontend (I deleted the translation of the term "Product Resources", ID 232). Please check the attached screenshot.

    Looking forward to your reply.
    Thanks

    Recursos de productos - L&W Supply 2024-03-04 10-16-05.jpg
    March 4, 2024 at 3:33 am #15367472

    kurtK-7

    How can I translate the terms without translating the taxonomy?

    March 4, 2024 at 6:49 am #15367565

    Long Nguyen
    Supporter

    Languages: English (English )

    Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

    I mean you shouldn't translate the term to display the fallback posts. If you translate the term, you have to translate the posts also to display posts in the Spanish language.

    I hope that makes sense.

    Resource Center Categories ‹ L&W Supply — WordPress 2024-03-04 13-48-03.png
    March 4, 2024 at 11:12 pm #15372314

    kurtK-7

    Please explain to me how this happens.

    In English All my posts show regardless of translation (grab1.png)
    hidden link

    In Spanish my posts in the ForEach Loop do not display BUT they do for While If Loop (grab3.png)
    hidden link

    Also in Spanish if I translate a single post then that translated post AS WELL AS all of my non translated posts show up under my translated taxonomy term (grab2.png)

    Why does translating a single post trigger the ForEach loop to look at all the non translated posts and display them under the translated taxonomy term?

    grab3.png
    grab2.png
    grab1.png
    March 5, 2024 at 4:23 am #15372507

    Long Nguyen
    Supporter

    Languages: English (English )

    Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

    Hi,

    I cannot replicate the issue on the sandbox site that I shared above. If I translate the category, the translation page doesn't display any translation post even if a post is translated.
    hidden link

    I'm asking our 2nd tier support about this case and update you here when I have something to share.

    Thanks for your patience.

    March 5, 2024 at 1:59 pm #15374863

    kurtK-7

    Thank you for bringing in tier 2 support. I'd like to point out 1 additional thing that may help them with their testing.
    In the above scenario we are targeting the children of a parent term which is why we have some of the scripting like this
    //$parent_term_id = apply_filters( 'wpml_object_id', 232, $taxonomy_slug, TRUE );
    $parent_term_id = 232;

    Also on my site I have Manufacturing Pricing Updates. This content is sorted by a custom Taxonomy called Product Categories. When I converted those taxonomy terms to Spanish everything still works even though the posts remain untranslated.

    Default Translation Terms with Default Translation Posts
    hidden link

    Translated Terms with Default Translation Posts
    hidden link

    This works perfectly and the big difference between this and the Product Resources Page is that my foreach loop goes through every parent level term vs the product resources page that goes through the child terms of a parent terms called out by the ID

    I hope this helps as well as demonstrates that a taxonomy can be converted and still display non translated posts inside a foreach loop.

    March 6, 2024 at 2:52 am #15376881

    Long Nguyen
    Supporter

    Languages: English (English )

    Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

    Hi,

    I will check both cases with parent term and child term on the sandbox site after having more information from the 2nd tier support.

    Thank you.

    March 11, 2024 at 1:43 pm #15395692

    kurtK-7

    Checking in as well as making sure this support ticket does not close from inactivity. Please let me know what you have found.

    March 12, 2024 at 1:23 am #15397570

    Long Nguyen
    Supporter

    Languages: English (English )

    Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

    Hi,

    Our 2nd tier support is still checking the issue. I will let you know once I have any information.

    Thanks.