I have a view listing all the items in a particular post type. The site has WPML installed.
When I include the view on a page in the default language, the view works fine, including a taxonomy query filter based on URL parameters.
When I include the view on a page translated into the second language, the view works fine without the URL parameters/taxonomy filter, but not with the filter.
In WPMLs "Language URL format" setting, I have chosen "Different languages in directories" (not checked "Use directory for default language ").
I have searched the forums for solutions. The closest thread, I've found is these ones:
https://toolset.com/forums/topic/views-filter-not-working-in-secondary-language/
https://toolset.com/forums/topic/views-filter-still-not-working-in-secondary-language/
But I don't see a solution there.
I turned on the Views debug tool (very useful) and found out that, in this situation, Views is sending the taxonomy terms in the German versions. German is the secondary language, with English being the default language.
Then I found out that I could do a workaround by using wpv_filter_query, like this:
if ( isset( $_GET['wpvprofession'] ) ) {
add_filter( 'wpv_filter_query', 'jakob_filter_allcourses', 20, 1 );
function jakob_filter_allcourses( $query_args ) {
if ( $query_args[tax_query][0][terms][0] == 31 ) {
$query_args[tax_query][0][terms][0] = 3;
} else if ( $query_args[tax_query][0][terms][0] == 32 ) {
$query_args[tax_query][0][terms][0] = 4;
} else if ( $query_args[tax_query][0][terms][0] == 33 ) {
$query_args[tax_query][0][terms][0] = 5;
} else if ( $query_args[tax_query][0][terms][0] == 34 ) {
$query_args[tax_query][0][terms][0] = 14;
}
if ( $query_args[tax_query][1][terms][0] == 26 ) {
$query_args[tax_query][1][terms][0] = 6;
} else if ( $query_args[tax_query][1][terms][0] == 27 ) {
$query_args[tax_query][1][terms][0] = 7;
}
if ( $query_args[tax_query][2][terms][0] == 28 ) {
$query_args[tax_query][2][terms][0] = 9;
} else if ( $query_args[tax_query][2][terms][0] == 29 ) {
$query_args[tax_query][2][terms][0] = 11;
} else if ( $query_args[tax_query][2][terms][0] == 30 ) {
$query_args[tax_query][2][terms][0] = 10;
}
return $query_args;
}
}
Basically I am exchanging the German term ids with the English ones and the end result is working for me. If you see any weaknesses in this approach, I welcome your feedback.