Skip to content

Instantly share code, notes, and snippets.

@pajtai
Last active April 4, 2018 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pajtai/dd61028765b439d8b9e93d67b90ea9d0 to your computer and use it in GitHub Desktop.
Save pajtai/dd61028765b439d8b9e93d67b90ea9d0 to your computer and use it in GitHub Desktop.
modify core query
<?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