Question

The below code returns all tax_terms found for the custom taxonomy "music_categories" (Rock, Jazz, Rap etc) and then arranges all posts of custom post type "music" under the corresponding headings.

But I need a little more filtering. Post type music has a second custom taxonomy called "portfolio" that has the terms "Portfolio One", "Portfolio Two" and "Portfolio Three".

I need to be able to select a portfolio before the below code starts running.

The output looks like this:

Jazz

post1, post4, post10

Rock

post4, post5, post6

Blues

post4, post10

etc...

        $post_type = 'music';
        $tax = 'music_categories';
        $tax_terms = get_terms($tax);

        if ($tax_terms) {
            foreach ($tax_terms as $tax_term) {
                $args=array(
                    'post_type' => $post_type,
                    "$tax" => $tax_term->slug,
                    'post_status' => 'publish',
                    'posts_per_page' => -1,
                    'caller_get_posts'=> 1
                    );

                $my_query = null;
                $my_query = new WP_Query($args);
                if( $my_query->have_posts() ) {
                    echo '<h1 class="taxonomy-heading">'. $tax_term->name . '</h1>';
                    while ($my_query->have_posts()) : $my_query->the_post(); ?>

                            // a post with given taxonomy (heading, paragraphs, images etc)

                    <?php
                    endwhile;
                }
                wp_reset_query();
            }
        }
        ?>
Was it helpful?

Solution

I was complicating this way to much in my mind. I added one line to the $args array and voila!

                $args=array(
                    'post_type' => $post_type,
                    "$tax" => $tax_term->slug,
                    'portfolios' => 'portfolio-two',
                    'post_status' => 'publish',
                    'posts_per_page' => -1,
                    'caller_get_posts'=> 1
                    );

Now I get list of all posts from post-type music that are marked portfolio-two (taxonomy). They get listed up under the genre taxonomy they are a part of.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top