I have a custom post type in my WordPress site for my portfolio projects. The structure is to have a portfolio index listing portfolio categories and then child pages for the individual projects.
On the portfolio index, I am trying to show each portfolio category (there's 4 in total) with its title and the number of projects (child pages) within it. e.g. Photography – 16 projects.
The part I am stuck on is counting the number of child pages for each parent category when cycling through the loop. I have the standard loop functioning correctly as this:
// The Query
$the_query = new WP_Query(array(
// Query only the custom post type ja_portfolio
'post_type' => 'ja_portfolio',
// Display only top-level parent pages (e.g. project categories)
'post_parent' => 0,
'orderby' => 'menu_order',
'order' => 'ASC'
));
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
// Other parts of the loop functioning fine...
// Get number of child pages *struggling here*
$children = get_pages('child_of='.$the_query->the_post());
if( count( $children ) != 0 ){
echo count($children);
};
// Rest of code functioning fine here...
} else {
// no posts found
// Restore original Post Data
wp_reset_postdata();
}
I have tried what seems like an endless amount of possibilities and feel as though I am close. The closest I can get is to display the same number on every category which appears to count all child pages and all categories, rather than just the count for the particular category being queried.
I have also tried all sorts of variations using the_post()
and theID()
or within each other and numerous others. Even tried swapping out get_pages()
for get_post()
and all variations I can think of without going round in circles, but I'm sure I must have missed the correct combination somewhere.