-
-
Save mvital/295099961fac1e121c69dee31cc361ac to your computer and use it in GitHub Desktop.
Custom tax query
This file contains hidden or 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_rewrite_rule( | |
'^result/([^/]*)/?([^/]*)$', | |
'index.php?pagename=result&style_slug=$matches[1]&gender_slug=$matches[2]', | |
'top' ); | |
add_rewrite_tag('%style_slug%','([^/]*)'); | |
add_rewrite_tag('%gender_slug%','([^/]*)'); |
This file contains hidden or 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 | |
// Determine filters | |
$style_slug = (get_query_var('style_slug')) ? get_query_var('style_slug') : 'all'; | |
$gender_slug = (get_query_var('gender_slug')) ? get_query_var('gender_slug') : 'all'; | |
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; | |
// Args for Query (below) | |
$args = array( | |
'suppress_filters' => 0, | |
'post_type' => 'post_type', | |
'showposts' => 10, | |
'paged' => $paged, | |
'orderby' => 'title', | |
'order' => 'ASC', | |
'tax_query' => array() | |
); | |
// FILTER: Style | |
if($style_slug!='all'){ | |
$style_query = array( | |
'taxonomy' => 'style', | |
'field' => 'slug', | |
'terms' => $style_slug | |
); | |
array_push($args['tax_query'], $style_query); | |
$thisstyle = get_term_by('slug', $style_slug, 'style'); | |
} | |
else { | |
$thisstyle = (object) ['name' => __('All', 'text-domain')]; | |
} | |
// FILTER: Gender | |
if($gender_slug!='all'){ | |
$gender_query = array( | |
'taxonomy' => 'gender', | |
'field' => 'slug', | |
'terms' => $gender_slug | |
); | |
array_push($args['tax_query'], $gender_query); | |
$thisgender = get_term_by('slug', $gender_slug, 'gender'); | |
} | |
else { | |
$thisgender = (object) ['name' => __('All', 'text-domain')]; | |
} | |
// RUN QUERY | |
$resultquery = new WP_Query( $args ); | |
$result = $resultquery->posts; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment