1

I currently have a custom taxonomy set up called development-category at the moment, if I want to set up a posts loop for each of these taxonomies, I'm having to do it as follows:

<?php $hotels_loop = new WP_Query( array( 'development-category' => 'hotels', 'post_type' => 'developments' ) ); ?>

    <ul class="sector-menu-sub-menu">       
        <?php while ( $hotels_loop->have_posts() ) : $hotels_loop->the_post(); ?>

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

        <?php endwhile; ?>
    </ul>
    <?php wp_reset_query(); ?>


<?php $leisure_loop = new WP_Query( array( 'development-category' => 'leisure', 'post_type' => 'developments' ) ); ?>

    <ul class="sector-menu-sub-menu">       
        <?php while ( $leisure_loop->have_posts() ) : $leisure_loop->the_post(); ?>

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

        <?php endwhile; ?>
    </ul>
    <?php wp_reset_query(); ?>


<?php $commercial_loop = new WP_Query( array( 'development-category' => 'commercial', 'post_type' => 'developments' ) ); ?>

    <ul class="sector-menu-sub-menu">       
        <?php while ( $commercial_loop->have_posts() ) : $commercial_loop->the_post(); ?>

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

        <?php endwhile; ?>
    </ul>
    <?php wp_reset_query(); ?>


//etc etc

This works, but I'm having to set up a loop for each taxonomy term, in this instance got a loop for the hotels taxonomy term, the leisure taxonomy term and the commercial taxonomy term, but I don't want to have to do this for every new taxonomy term I add, and as the site is content managed, it will be managed by the client so they won't be able to do so either. I'm wondering if there's a way too set up a loop for every taxonomy term for the development-category taxonomy, so if a new term is added, it will create a new loop as per my code below, if that makes sense? Any suggestions would be greatly appreciated!

0

You can get all the terms in a custom taxonomy using the following code:

$terms = get_terms( 'development-category' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    foreach ( $terms as $term ) {
        $loop = new WP_Query( array( 'development-category' => $term->slug, 'post_type' => 'developments' ) ); ?>
        ....    
    }
}
  • This can get very expensive – Pieter Goosen Sep 3 '15 at 9:55

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.