Note that there are some explanatory texts on larger screens.

plurals
  1. POWordpress list all posts grouped by tag for a given category
    text
    copied!<p>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.</p> <p>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").</p> <p>So in the reverse, I need to get all items category "restaurants" and then group them by tag (e.g. "torrington", "danbury" etc.).</p> <pre><code>&lt;?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-&gt;term_id; // create a custom wordpress query query_posts("cat=$cat_id&amp;tag=torrington&amp;post_per_page=100"); // start the wordpress loop! if (have_posts()) : // Make a header for the category echo '&lt;h2 class="cat-title"&gt;'.$cat-&gt;name.'&lt;/h2&gt;'; while (have_posts()) : the_post(); ?&gt; &lt;?php // create our link now that the post is setup ?&gt; &lt;div class="listing"&gt; &lt;a href="&lt;?php the_permalink();?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt; &lt;?php the_content(); ?&gt; &lt;/div&gt; &lt;?php //echo '&lt;hr/&gt;'; ?&gt; &lt;?php endwhile; endif; // done our wordpress loop. Will start again for each category ?&gt; &lt;?php } // done the foreach statement ?&gt; </code></pre> <p>EDIT: I've gotten this far, but the query_posts statement seems to return nothing:</p> <pre><code> &lt;?php // get all the categories from the database $tags = get_tags(); // loop through the categries foreach ($tags as $tag) { echo($tag-&gt;name); // setup the cateogory ID $tag_id = $tag-&gt;term_id; echo($tag_id); // create a custom wordpress query query_posts("tag_id=$tag_id&amp;cat=eats&amp;post_per_page=100"); // start the wordpress loop! if (have_posts()) : echo('posts'); // Make a header for the category echo '&lt;h2 class="cat-title"&gt;'.$tag-&gt;name.'&lt;/h2&gt;'; while (have_posts()) : the_post(); ?&gt; &lt;?php // create our link now that the post is setup ?&gt; &lt;div class="listing"&gt; &lt;a href="&lt;?php the_permalink();?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt; &lt;?php the_content(); ?&gt; &lt;/div&gt; &lt;?php //echo '&lt;hr/&gt;'; ?&gt; &lt;?php endwhile; endif; // done our wordpress loop. Will start again for each category ?&gt; &lt;?php } // done the foreach statement ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload