Skip to content

Instantly share code, notes, and snippets.

@kricore
Created December 16, 2016 11:43
Show Gist options
  • Save kricore/010ff9f9f8597c33ed3927c0620576d0 to your computer and use it in GitHub Desktop.
Save kricore/010ff9f9f8597c33ed3927c0620576d0 to your computer and use it in GitHub Desktop.
Filter the correct language on WordPress archive pages based on the lang $_GET param.
<?php
/**
* Language Detection
* @param $query,
* @uses pre_get_posts filter
* @return the modified query
*/
function kdk_exclude_category( $query ) {
if ( ($query->is_tag() && $query->is_main_query()) ||
($query->is_search() && $query->is_main_query()) ||
($query->is_author() && $query->is_main_query()) ||
($query->is_date() && $query->is_main_query()))
{
// Get the $GET Param
if(isset($_GET["lang"]))
{
$langcode = $_GET["lang"];
$query->set( 'category_name', $langcode );
}
return;
}
}
add_action( 'pre_get_posts', 'kdk_exclude_category' );
@kricore
Copy link
Author

kricore commented Dec 16, 2016

This should be placed in your theme's functions.php file
The language code is the parent category (eg: en - de etc..) so we basically filter posts per category.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment