Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8927293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:11:25+00:00 2026-06-15T08:11:25+00:00

I have a loop for getting all posts grouped by category for a given

  • 0

I have a loop for getting all posts grouped by category for a given tag (see code below). I need to turn it around and do the exact same thing but by tags for a given category.

In the code sample I am getting all posts tagged “torrington” and then doing a loop to display them grouped by category with an H2 of the category (e.g. “restaurants”).

So in the reverse, I need to get all items category “restaurants” and then group them by tag (e.g. “torrington”, “danbury” etc.).

<?php           
        // get all the categories from the database
        $cats = get_categories(); 
            // loop through the categries
            foreach ($cats as $cat) {
                // setup the cateogory ID
                $cat_id= $cat->term_id;

                // create a custom wordpress query
                query_posts("cat=$cat_id&tag=torrington&post_per_page=100");

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

                // Make a header for the category
                echo '<h2 class="cat-title">'.$cat->name.'</h2>';

                while (have_posts()) : 
                the_post(); ?>

                    <?php // create our link now that the post is setup ?>
                    <div class="listing">
                    <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
                    <?php the_content(); ?>
                    </div>
                    <?php //echo '<hr/>'; ?>

                <?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
            <?php } // done the foreach statement ?>

EDIT: I’ve gotten this far, but the query_posts statement seems to return nothing:

 <?php           
        // get all the categories from the database
        $tags = get_tags(); 
            // loop through the categries
            foreach ($tags as $tag) {
                echo($tag->name);
                // setup the cateogory ID
                $tag_id = $tag->term_id;

                echo($tag_id);

                // create a custom wordpress query
                query_posts("tag_id=$tag_id&cat=eats&post_per_page=100");

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

                echo('posts');
                // Make a header for the category
                echo '<h2 class="cat-title">'.$tag->name.'</h2>';

                while (have_posts()) : 
                the_post(); ?>

                    <?php // create our link now that the post is setup ?>
                    <div class="listing">
                    <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
                    <?php the_content(); ?>
                    </div>
                    <?php //echo '<hr/>'; ?>

                <?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
            <?php } // done the foreach statement ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:11:26+00:00Added an answer on June 15, 2026 at 8:11 am

    Got it. It was simple change of tag_id=$tag_id not tag=$tag_id.

    Code above updated. Relevant line is query_posts("tag_id=$tag_id&cat=eats&post_per_page=100");

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a category template in Wordpress for all posts that are in
I have an nsthread, a while loop within this one. It's getting objects from
I'm getting caught up on PHP again, and have the following update code. This
UPDATE 1: I also have the following foreach loop in my code: foreach ($_POST
I have a loop, that goes through some entries. In the loop I'm getting
I have this php code. As you can see i query a mysql database
I have loop designed to validate the user input on a question, it was
I have loop for: for (int i = 1; i < 2000; i++) How
I have loop and each iteration of Hash looks like this: [1, {:clid=>1, :nvz=>4,
I'm pretty new to jQuery/JS. But I have loop in jQuery where I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.