Skip to content

Instantly share code, notes, and snippets.

@dospuntocero dospuntocero/complex query
Last active Jun 13, 2019

Embed
What would you like to do?
i have a product with 2 taxonomies and i need to filter the ones that belongs to one specific taxonomy and at the same time, order the objects by category
<?php
/**
* Template Name: Product
*/
get_header();
?>
<main class="wrapper">
<?php $product_category_to_show_ids = get_field('product_category_to_show'); ?>
<?php
$custom_post_type = 'product-series';
$category = 'product_category';
$type = 'product_type';
$terms = get_terms([
'taxonomy' => $category,
'hide_empty' => true
]);
foreach ($terms as $term) :
$cat_posts_args = array(
'post_type' => $custom_post_type,
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => $category,
'field' => 'id',
'terms' => $term->term_id,
'include_children' => false
),
array(
'taxonomy' => $type,
'field' => 'term_id',
'terms' => $product_category_to_show_ids->term_id,
'include_children' => false
)
)
);
$cat_posts = new WP_Query($cat_posts_args);
if ($cat_posts->have_posts()) :
?>
<div id="<?php echo $term->slug; ?>" class="section-head grid-x grid-padding-x">
<div class="cell medium-10 medium-offset-1">
<h2>
<?php echo $term->name; // print the category name
?>
</h2>
</div>
</div>
<?php
while ($cat_posts->have_posts()) : $cat_posts->the_post();
get_template_part('partials/product'); //include the product template here
endwhile;
endif;
wp_reset_postdata();
endforeach;
?>
</main>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.