Last active
April 4, 2018 14:56
-
-
Save pajtai/dd61028765b439d8b9e93d67b90ea9d0 to your computer and use it in GitHub Desktop.
modify core query
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 | |
add_action('pre_get_posts', function($query) { | |
if ($query->is_home() && $query->is_main_query()) { | |
$query->set('posts_per_page', 8); | |
$currentMarket = get_query_var('market'); | |
$currentCategory = get_query_var('category'); | |
$taxQuery = getTaxonomyQuery($currentMarket, $currentCategory); | |
if ($taxQuery) { | |
$query->set('tax_query', $taxQuery); | |
} | |
} | |
}); | |
function getTaxonomyQuery($currentMarket, $currentCategory) { | |
if (!$currentMarket && !$currentCategory) { | |
return false; | |
} | |
$taxQuery = array(); | |
if ($currentMarket && $currentCategory) { | |
$taxQuery['relation'] = 'AND'; | |
} | |
if ($currentMarket) { | |
array_push($taxQuery, array( | |
'taxonomy' => 'market', | |
'field' => 'slug', | |
'terms' => $currentMarket, | |
)); | |
} | |
if ($currentCategory) { | |
array_push($taxQuery, array( | |
'taxonomy' => 'category', | |
'field' => 'slug', | |
'terms' => $currentCategory, | |
)); | |
} | |
return $taxQuery; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment