Site icon Hip-Hop Website Design and Development

Group posts in a category based on tags in custom taxonomy

I had my built-in posts set up with categories and tags. I created a template that would display posts of the category that matched the page slug.

then it would group them by the Tags. for example, if the slug was “restaurants” it would show all the restaurants in the database. The restaurant posts were tagged with town name tags. So they would be grouped by towns in this loop, and each town (tag) would be an H2. It worked fine.

However, I am re-organizing and moved all the town tags from the default “tags” taxonomy to a custom taxonomy called “towns”. This broke my template and I’m not sure how to fix it. I tried using get_terms instead of get_tags but it did not work.

            <?php
            $slug = the_slug();

            // get all the tags from the database
            $tags = get_tags();
            // loop through the categries
            foreach ($tags as $tag) {
                // setup the category ID
                $tag_id = $tag->term_id;

                query_posts( array ( 'tag_id' => $tag_id, 'category_name' => $slug, 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => -1 ) );

                // start the wordpress loop!
                if (have_posts()) :

                // Make a header for the category, only if there are posts under it
                echo '<h2 class="group-title">'.$tag->name.'</h2>';

                while (have_posts()) : the_post(); 
                    get_template_part('template-parts/place-listing', 'place');
                endwhile; endif; // done our wordpress loop. Will start again for each category 
            } // done the foreach statement 
            ?>

UPDATE

Here is the page displayed with the above code. https://litchfieldmagazine.com/arts/

It was originally built with the default post type and its categories and tags. I am looking to enhance the site and have used a plugin to move the default tags into a custom taxonomy “towns”. I confirm the posts are all correctly tagged with this new taxonomy.

UPDATE 2

I think I finally have it — at least with a hard-coded category of “Arts”. Not sure if this is the optimal or correct way. Now I just need to get the current page slug (which in this case is “arts”)

    <?php
                // get all the town tags from the database
                $towns = get_terms( array(
                        'taxonomy' => 'town',
                        'hide_empty' => 1,
                ) );

                foreach ($towns as $town) {
                    // echo '<h3>' . $town->name. '</h3>';

                    $args = array(
                    'post_type' => 'post', 
                    'orderby' => 'title', 
                    'order' => 'ASC', 
                    'posts_per_page' => -1,
                    'tax_query' => array(
                        'relation' => 'AND', // at the same time meet both conditions
            array(
                'taxonomy' => 'town', // custom tag taxonomy name
                'field'    => 'term_id',
                'terms'    => array( $town->term_id ),
            ),
            array(
                'taxonomy' => 'category', // default taxonomy category name
                'field'    => 'slug',
                'terms'    => array( 'arts' ),
            ),
                    ),
                );

                $query = new WP_Query( $args );
                    if ($query->have_posts()) :

                        echo '<h2 class="group-title">' . $town->name. '</h2>';

                            while ($query->have_posts()) : $query->the_post(); 
                                get_template_part('template-parts/place-listing', 'place');
                            endwhile;

                            wp_reset_postdata(); 

                    endif;          
                }
            ?>