0

I am trying to show items on a page grouped by child taxonomies in something that looks like this...

Child Taxonomy 1 (h2)

Item 1
Item 2

Child Taxonomy 2 (h2)

Item 3
Item 4

I did manage to get the children of a taxonomy and get their posts, unfortunately when using the following code I get all parent taxonomy posts twice rather than have them grouped by their respective child taxonomy like...

Child Taxonomy 1 (h2)

Item 1
Item 2
Item 3
Item 4

Child Taxonomy 2 (h2)

Item 1
Item 2
Item 3
Item 4

I am at a bit of a loss as to what I am doing wrong with my code that makes the posts show up under both groups rather than only under the child taxonomy to which they belong and would greatly appreciate a pointer to what I am missing.

<?php
    $termID = 8; // Parent A ID
    $taxonomyName = "product_category";
    $termchildren = get_term_children( $termID, $taxonomyName ); ?>
<?php        
          foreach ($termchildren as $child) {
              $term = get_term_by( 'id', $child, $taxonomyName );
?>

<h2><?php echo $term->name; ?></h2>             

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query = new WP_Query( array(
                        'post_type' => 'product', 
                        'product_category' =>$term->name,
                        'meta_query' => array(
                            'relation' => 'AND',
                            array(
                                'key' => 'product_active',
                                'value' => '1'
                            ),
                        ),
                        'posts_per_page' => -1,
                        'orderby' => 'menu_order',
                        'order' => 'ASC',
                        'paged'=> $paged

                        )); ?>


<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

<?php the_title(); ?> <img src="<?php $image = wp_get_attachment_image_src(get_field('product_image'), 'full'); ?><?php echo $image[0]; ?>" alt="<?php the_title(); ?>" /><br />
<?php the_content(); ?>


<?php endwhile; 
      wp_reset_query();
?>  


<?php
unset($term);
}
?>  
  • Can a product come under both parent and child category? Whats the use of 'relation' => 'AND' in the meta_query? Avoid using $wp_query variable as this is a global one. – Chittaranjan May 21 '14 at 3:18
  • The code'relation' => 'AND'code part is there to ensure that only the items that are belonging to said taxonomy 'AND' are set to active will show up. This way you can have a post not show just by unchecking a check box rather than by deleting it. – shufflepath May 21 '14 at 3:23
  • The products would only come under the child category. – shufflepath May 21 '14 at 3:30
  • Nope. I don't think the relation has any use here as you have only one array here. It's for the relationship between each meta_query inner array. Please check the Codex – Chittaranjan May 21 '14 at 5:37
0

Few things -

Relation is not needed.

  1. It only works between multiple's of the same type of query (meta OR tax) it explicitly says in the codex under custom_meta queries to not use this with a single inner meta query

  2. Even if you had 2 queries AND is considered the default you really only need to specify OR

Your method of querying the taxonomy is wrong for 1.5 reasons

  • Definitely wrong: you're using the term's name property when by default it queries by slug
  • Half wrong: this method has been depreciated in favour of tax_query

Other things:

You're using a variable that should be reserved for the main query

You're resetting the query when all you need to be doing is resetting the post data

Unset does not need to be called on $term.

Also do you really want pagination? from your explanation it doesn't sound like you do, but that's easily changed..

Here's some code with a few updates, let me know how it goes.

<?php
    $termID = 8; // Parent A ID
    $taxonomyName = "product_category";
    $termchildren = get_term_children( $termID, $taxonomyName );

          foreach ($termchildren as $child) {
              $term = get_term_by( 'id', $child, $taxonomyName );

echo "<h2>".$term->name."</h2>";             

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$my_query = new WP_Query( array(
                        'post_type' => 'product', 
                        'tax_query' => array(
                            array(
                                'taxonomy' => $taxonomyName,
                                'field' => 'slug',
                                'terms' => $term->slug
                            )
                        ),
                        'meta_query' => array(
                            array(
                                'key' => 'product_active',
                                'value' => '1'
                            ),
                        ),
                        'posts_per_page' => -1,
                        'orderby' => 'menu_order',
                        'order' => 'ASC',
                        'paged'=> $paged

                        )); ?>


<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

<?php the_title(); ?> <img src="<?php $image = wp_get_attachment_image_src(get_field('product_image'), 'full'); ?><?php echo $image[0]; ?>" alt="<?php the_title(); ?>" /><br />
<?php the_content();

    endwhile; 
      wp_reset_postdata();
}
?>
  • Thanks a lot for the comments, the code you provided made it do exactly what I wanted aj-adl and I thank you very much for helping, I will make sure to look into tax_query for future reference. – shufflepath May 21 '14 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.