I have a custom page template. I am calling a wp_query based on an Item's ID that displays information about that Item. I then call another wp_query that returns records related to that item grouped by category.
For example, I get 6 related records, 2 in each category. I would also like to display the record count for each category. I want to loop through my single wp_query record set and display each "category" of records as a subset on the page without calling the database again based on each category.
For example ...
Related to Item 1
Category 1 (count = 2)
- Record 1
- Record 2
Category 2 (count = 2)
- Record 3
- Record 4
Category 3 (count = 2)
- Record 5
- Record 6
Code Below ...
$args = array(
'taxonomy' => 'mediacategory',
'orderby' => 'name',
'hide_empty' => 0,
'hierarchical' => 0
$taxonomies = get_categories( $args );
$catToCompare = "";
if ($taxonomies) :
foreach ($taxonomies as $taxonomy) :
$catToCompare = $taxonomy->cat_name;
echo '<p>' . $catToCompare . '</p>';
while ( $connectedRecords->have_posts() ) :
$mediaCat = get_the_term_list($post->ID, 'mediacategory');
$mediaCat = strip_tags($mediaCat);
if ($mediaCat == $catToCompare) :
echo '<p>' .the_title(). '</p>';
What I really want to do is take my $connectedRecords and create subsets of records based on category if possible? I would like to eliminate the repetitive looping by category.
I think I am close? I just can't get the record count by category. Is there an easier way to do this?
Thanks in advance!