Created
January 8, 2018 12:18
-
-
Save samikeijonen/4e1e82ce250d8be6497116d466c1c685 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function valteri_pre_get_posts( $query ) { | |
// Show only posts from 'blogi' category. | |
if ( ! is_admin() && $query->is_main_query() && is_home() ) { | |
// Get Finnish ID. | |
$lang_fi = get_term_by( 'slug', 'fi', 'language' ); | |
$id_fi = $lang_fi->term_id; | |
// Get Swedish ID. | |
$lang_sv = get_term_by( 'slug', 'sv', 'language' ); | |
$id_sv = $lang_sv->term_id; | |
// Show both languages. | |
$tax_query = array( | |
array( | |
'taxonomy' => 'language', | |
'field' => 'term_taxonomy_id', | |
'terms' => array( $id_fi, $id_sv ), | |
), | |
); | |
$query->set( 'tax_query', $tax_query ); | |
// For some reason we need this that also Swedish blog page shows both languages. | |
$query->set( 'lang', 'fi, sv' ); | |
// Show only posts from these categories. | |
$query->set( 'category__in', array( get_cat_ID( 'Blogi' ), get_cat_ID( 'Blog' ) ) ); | |
} | |
} | |
add_action( 'pre_get_posts', 'valteri_pre_get_posts' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment