0

I have 4 custom taxonomies with multiple terms for a "Members" page. I have a taxonomy of "location" and "organize" that I'm trying to separate into a list. I need to grab all posts by the "aba-therapists" term in the "organize" taxonomy and separate those by their "locations." If that makes sense to anyone. Something like this:

ABA Therapist Term of Organize Taxonomy

Term 1 of Location Taxonomy

Post Post Post Post

Term 2 of Location Taxonomy

Post Post Post Post

Term 3 of Location Taxonomy

Post Post Post Post

This is the code I have, which just pulls in every post within the "members" CPT and organizes by every term:

    foreach ($taxonomies as $taxonomy){
        $terms = get_terms($taxonomy, array('orderby' => 'date', 'order' => 'ASC'));

        if ( !empty( $terms ) && !is_wp_error( $terms ) ){
            foreach ( $terms as $term ) {
                $args = array(
                    'post_type'           => $post_type,
                    'orderby'             => 'date',
                    'order'               => 'ASC',
                    'ignore_sticky_posts' => 1,
                    'post_status'         => 'publish',
                    'posts_per_page'      => - 1,
                    'tax_query'           => array(
                        array(
                            'taxonomy'    => $taxonomy,
                            'field'       => 'slug',
                            'terms'       => $term->slug
                        )
                    )
                );
                $my_query = null;
                $my_query = new WP_Query($args);
                if ($my_query->have_posts()) {
                    echo '<div class="members"><div class="wrap"><h3>' . $term->name . '</h3></div><div class="details"><div class="wrap">';

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

                            <a href="<?php the_permalink(); ?>">
                                <?php if ( has_post_thumbnail() ) : ?>
                                    <?php the_post_thumbnail('cac-small'); ?>
                                <?php else : ?>
                                    <img src="<?php echo get_template_directory_uri(); ?>/img/profile-placeholder.png" alt="Default Image">
                                <?php endif; ?>
                                <div class="caption">
                                    <p><?php the_title(); ?></p>
                                </div>
                            </a>

                        <?php
                        endwhile; 
                } // END if have_posts loop
                    echo '</div></div></div>'; // Close 'details', 'wrap', & 'members' DIVs
                wp_reset_query(); 
            } // END foreach $terms
        }
    }
?>

I'm lost on where to start. Anyone have any clues?

1 Answer 1

1
$terms = get_terms('location', array('orderby' => 'date', 'order' => 'ASC'));

foreach( $terms as $term ) {
    $args = array(
        'post_type'           => $post_type,
        'orderby'             => 'date',
        'order'               => 'ASC',
        'ignore_sticky_posts' => 1,
        'post_status'         => 'publish',
        'posts_per_page'      => - 1,
        'tax_query'           => array(
            'relation' => 'AND',
            array(
                'taxonomy'    => 'organize',
                'field'       => 'slug',
                'terms'       => array( 'aba-therapist' )
            ),
            array(
                'taxonomy'    => 'location',
                'field'       => 'slug',
                'terms'       => array( $term->slug )
            )
        )
    );

    $my_query = new WP_Query($args);
    if ($my_query->have_posts()) {
        echo '<div class="members"><div class="wrap"><h3>' . $term->name . '</h3></div><div class="details"><div class="wrap">';

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

                <a href="<?php the_permalink(); ?>">
                    <?php if ( has_post_thumbnail() ) : ?>
                        <?php the_post_thumbnail('cac-small'); ?>
                    <?php else : ?>
                        <img src="<?php echo get_template_directory_uri(); ?>/img/profile-placeholder.png" alt="Default Image">
                    <?php endif; ?>
                    <div class="caption">
                        <p><?php the_title(); ?></p>
                    </div>
                </a>

            <?php
            endwhile; 
    } // END if have_posts loop
    wp_reset_postdata();
}

Hope this code fill work for you. add ABA therapist header above.

3
  • I see what you did and thanks. Gives me a better understanding here. But, it's not pulling any the posts. Any idea? Commented Jul 22, 2014 at 18:43
  • check if the term slug and taxonomy names are correct.
    – sabarnix
    Commented Jul 23, 2014 at 5:34
  • Wow! Just missing an "s" on a term. Thank you!! Commented Jul 23, 2014 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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