0

I'm trying to loop through the various posts that fit within my custom taxonomy: "industries". The post type is custom (type: "member") and so I've been trying to build a loop that outputs the current taxonomy header and then the businesses that have that category assigned to them. Like so:

Business Services:

Company A

Construction Services:

Company B

Except with my current code it outputs all businesses under each heading. I'm sure the logic is just not quite right, but I am trying to figure out what I've done wrong and I can't see it.

My archive-members.php code:

  $categories = get_terms('industries', array(
    'hide_empty' => 1
  ));

  foreach($categories as $category) {

    $post_array = get_posts(array('post_type' => 'members', 'industries' => $category->name));
    setup_postdata($post_array);
    ?>

    <h1><?php echo $category->name; ?></h1>

    <?php
      if(have_posts()):
        while(have_posts()):
          the_post();
    ?>

    <p> <?php the_title(); ?> </p>

    <?php
    endwhile; endif;
    wp_reset_postdata();
  }
  • 1
    The example for get_posts shows how it is used. You're currently not outputting any of those results, and are just running the main query loop over and over for each of your categories. – Milo Dec 19 '16 at 16:21
  • Oh! I'll have a read, thank you. If you'd like to do up a quick answer replacing my main loop code with the proper methods from the object returned by get_posts I'd be happy to mark it correct. – armadadrive Dec 19 '16 at 16:34
1

I would do it like this, with WP Query. As @Milo pointed out, the issue is that the above code is running the main WP loop on each successive iteration and that loop isn't picking up on the custom query. Creating a loop from WP Query will yield the right results.

<?php 

$categories = get_terms('industries', array(
    'hide_empty' => 1
));

foreach($categories as $category) { ?>
    <h1><?php echo $category->name; ?></h1>
    <?php
    // WP_Query arguments
    $args = array(
        'post_type'              => array( 'members' ),
        'industries'          => $category->name,
    );

    // The Query
    $query = new WP_Query( $args );

    // The Loop
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            ?>
            <p> <?php the_title(); ?> </p>
            <?php
        }
    } else {
        // no posts found
    }

    // Restore original Post Data
    wp_reset_postdata();
}
    ?>
  • Actually, using my custom taxonomy name was working fine (otherwise the $categories array/object would just be empty. But as pointed out by @Milo above, I was using the main query loop without the affected results. – armadadrive Dec 20 '16 at 12:23
  • This does look like the right approach to The Loop, so if you want to amend this (and it tests alright) I'll mark it correct. But this is the query that is working for me: $post_array = get_posts(array('post_type' => 'members', 'industries' => $category->name)); – armadadrive Dec 20 '16 at 12:28
  • Then maybe using 'tax_query' => array( array( 'taxonomy' => 'industries', 'field' => 'slug', 'terms' => $category->name, ), ) instead of 'category_name' => $category->name, – dgarceran Dec 20 '16 at 13:15
  • I tried your code and it works great. Thanks for the answer! I did make a minor edit as 'industries' => $category->name works fine. – armadadrive Dec 20 '16 at 13:19
  • I'm glad it helped you! Are you sure? I haven't done that ever haha Shouldn't it be tax_query instead of only 'industries'? If it works for you then I'll approve your edit! – dgarceran Dec 20 '16 at 13:34

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.