Filter Data From a Single WP_Query Result Set
-
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 …
Item 1
Item Name
Item DescriptionRelated to Item 1
Category 1 (count = 2)
– Record 1
– Record 2Category 2 (count = 2)
– Record 3
– Record 4Category 3 (count = 2)
– Record 5
– Record 6Code 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() ) :
$connectedRecords->the_post();$mediaCat = get_the_term_list($post->ID, ‘mediacategory’);
$mediaCat = strip_tags($mediaCat);if ($mediaCat == $catToCompare) :
echo ‘<p>’ .the_title(). ‘</p>’;
endif;
endwhile;
endforeach;
endif;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!
- The topic ‘Filter Data From a Single WP_Query Result Set’ is closed to new replies.