• sam

    (@samjoelnang)


    The WordPress website I’m working on is available in two languages using Polylang, en-US and en-PH. I use CPT UI and have a custom taxonomy “subtopic” that is not multilingual. Under “subtopic” are terms like “copyright” and “trademark“.

    I intend to do the following:

    1. If a post with the current term is available in the main language, hide the other translation, but if it is not available in the main language, show it as is (in the secondary language). I am able to do this with the below code added as a snippet:
    /*
    * Query all posts versions on taxonomy archive page.
    */
    function sam_modify_taxonomy_query( $query ) {
    if ( function_exists( 'pll__' ) && ! is_admin() && $query->is_tax() ) {
    $query->set('tax_query', '');
    $query->set('lang', '');
    }
    }
    add_action( 'pre_get_posts', 'sam_modify_taxonomy_query' );
    1. Display only posts with the current taxonomy term on its archive page just like how categories and tags work. [This is the problem]

    Below is the code I’m using in the file taxonomy-subtopic.php to achieve both, but I end up getting all posts that are assigned any “subtopic” instead of getting only the posts with the current term (e.g., “copyright” or “trademark“) on the archive page.

    When readers visit the “copyright” archive page, for example, they should see only posts that have the term; in the main language or the secondary language if a main language version is not available.

    <?php if (have_posts()) : ?>

    <header class="header-title-wrapper">
    xxx
    </header><!-- .header-title-wrapper -->

    <h2 class="widget-title"><span>All articles</span></h2>

    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    query_posts(array(
    'post_type' => 'post',
    'lang' => 'en-PH', // force querying the PH posts
    'showposts' => -1,
    'tax_query' => array(
    array(
    'taxonomy' => 'subtopic',
    'field' => 'slug',
    'terms' => $term->term_id,
    'operator' => 'IN'
    )
    )
    )
    ); ?>

    <?php
    /* Start the Loop */
    while (have_posts()) : the_post(); ?>
    <?php global $post;
    if($post_id = pll_get_post($post->ID, pll_current_language())) { // get translated post (in current language) if exists
    $post = get_post($post_id);
    setup_postdata($post);
    }?>
    <?php
    get_template_part('template-parts/content', get_post_format());

    ?>
    <?php endwhile; ?>

    I would appreciate any advice or explanation that may help fix this. Thank you!

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I assume “lang” is valid taxonomy slug? While it’ll work as its own query var, that usage has been deprecated. While it currently still works as intended, it may not in the future. It’s preferred you include args for “lang” in your “tax_query” array.

    If the query you’re setting up is for the URL sample you’ve provided here, there will be no query var values for “term” or “taxonomy”, hence $terms will have an invalid value and your “tag_query” arg will be ineffective. The requested “copyright” term will be under query var “subtopic” (yes, in violation of the deprecation I mentioned above). No other query vars would be set based on that request alone. Of course your code sets other query vars, that’s fine.

    If you require “term” and “taxonomy” query vars to have values, you must include them in the request as a query string. For example: index.php?taxonomy=subtopic&term=copyright
    Not the most SEO friendly URL. Your sample URL is better for that, so get query var “subtopic” and use the returned value directly as the “terms” arg in “tax_query”.

    You could have worked this out for yourself if you did some basic debugging by dumping out various variable values. Something there is not what you expected. If you had output $terms, you would have found it’s not the term object you thought it was. If you dumped out the global $wp_query->query_vars array after getting posts you would see which query vars you actually have to work with.

    It’s actually discouraged for us to use query_posts() because it’s inefficient and wasteful. Better would be to alter the main query for copyright termed posts through the “pre_get_posts” action hook. First verify that it’s not an admin request and it’s the main query. Then verify it’s a subtopic term request. If all correct, alter the query by setting or unsetting query vars as needed. Then you can use the default archive template of your theme to display the posts you want displayed. No need for query_posts() or any kind of custom query. Doing it this way also makes pagination easier to accomplish since most pagination functions assume they are paginating the main query.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.