0

I have a query based on two parameters on a multilingual website (with WPML). To simplify, let's say that one of my filter is the language of the post to retrieve : English, French or all languages. English and French are working fine but I can't make the "all languages" filter work.

Here is my code:

<?php
// First I get the "lang" Get value
$langGet = get_query_var('lang', ICL_LANGUAGE_CODE);
?>

/* Then  have my form to filter posts */
<form action="<?php echo get_permalink( get_the_ID() ); ?>" method="get">
    <label for="lang"><?php _e( 'Languages', 'mytextdomain' ); ?></label>
    <?php
    $languages = apply_filters( 'wpml_active_languages', NULL );
    if($languages) { ?>
        <select name="lang">
            <?php foreach($languages as $language) : ?>
                <option value="<?php echo $language['language_code']; ?>" <?php echo ( $langGet == $language['language_code'] ) ? 'selected' : ''; ?>><?php echo $language['translated_name']; ?></option>
            <?php endforeach; ?>
            <option value="all" <?php echo ( $langGet == 'all' ) ? 'selected' : ''; ?>><?php _e('All languages', 'mytextdomain'); ?></option>
        </select>
    <?php } ?>
    <input type="submit" value="<?php _e( 'Filter projects', 'mytextdomain' ); ?>">
</form>

<?php
// Then I set up the switch for current language
if($langGet != 'all') {
    global $sitepress;
    $current_lang = $sitepress->get_current_language();
    $sitepress->switch_lang($langGet, true);
}

// Then I define my query
// Here is the line that is supposed to show all languages but it's not working.
$filters = ( $langGet == 'all' ) ? true : false;

$taxId = apply_filters( 'wpml_object_id', 755, 'project_category' );

$paged = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1;

$args = array(
    'posts_per_page' => 16,
    'post_type' => 'projects',
    'tax_query' => array(
        array(
            'taxonomy' => 'project_category',
            'field' => 'term_id',
            'terms' => $taxId,
        )
    ),
    'orderby'   => 'date',
    'order' => 'ASC',
    'paged' => $paged,
    'suppress_filters' => $filters
);
$projects = new WP_Query($args); 

But this is not showing all languages. Moreover I noticed that a #038;lang=all gets added at the end of my url when I am on page 2.

Can someone help me on this? Thanks!

1
  • 1
    Downvoting a question without explanation...what's the point guys?
    – Pipo
    Feb 14, 2018 at 9:48

2 Answers 2

2

So I've made it work. The problem was not with the filter but with tax query. Here is the solution for those who might be interested.

<?php
// I check the lang query var if it's empty we'll display all languages
$langGet = get_query_var('lang', 'all');
?>

/* Then  have my form to filter posts */
<form action="<?php echo get_permalink( get_the_ID() ); ?>" method="get">
    <label for="lang"><?php _e( 'Languages', 'mytextdomain' ); ?></label>
    <?php
    $languages = apply_filters( 'wpml_active_languages', NULL );
    if($languages) { ?>
        <select name="lang">
            <?php foreach($languages as $language) : ?>
                <option value="<?php echo $language['language_code']; ?>" <?php echo ( $langGet == $language['language_code'] ) ? 'selected' : ''; ?>><?php echo $language['translated_name']; ?></option>
            <?php endforeach; ?>
            <option value="all" <?php echo ( $langGet == 'all' ) ? 'selected' : ''; ?>><?php _e('All languages', 'mytextdomain'); ?></option>
        </select>
    <?php } ?>
    <input type="submit" value="<?php _e( 'Filter projects', 'mytextdomain' ); ?>">
</form>

<?php
// Then I set up the switch for current language
if( $langGet != 'all' ) {
    global $sitepress;
    $current_lang = $sitepress->get_current_language();
    $sitepress->switch_lang($langGet, true);
    $taxTerms = array( apply_filters( 'wpml_object_id', 755, 'project_category' ) );
    $filters = false;
} else {
    // If we want to display all languages we have to remove filters
    global $sitepress;
    remove_filter( 'get_terms_args', array( $sitepress, 'get_terms_args_filter' ), 10 );
    remove_filter( 'get_term', array( $sitepress, 'get_term_adjust_id' ), 1 );
    remove_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ), 10 );
    $taxTerms = array( 755, 776, 777, 778 );
    $filters = true;
}

$paged = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1;

// Then we set the query
$args = array(
    'posts_per_page' => $postsperpage,
    'post_type' => 'projects',
    'tax_query' => array(
      array(
          'taxonomy' => 'project_category',
          'field' => 'term_id',
          'terms' => $taxTerms
      )
    ),
    'orderby'   => $orderby,
    'order' => $order,
    'meta_key' => $metakey,
    'paged' => $paged,
    'suppress_filters' => $filters
);
$projects = new WP_Query($args); 
0

Please check this article from wpml

https://wpml.org/forums/topic/problems-to-show-in-all-languages-suppress_filters-true-not-working/

1
  • Thanks but I have already seen this post and this doesn't solve my issue.
    – Pipo
    Feb 14, 2018 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.