0

All I need is to get a number of children pages of a custom post type This is my query:

$args = array(
    'post_parent' => $post->ID,
    'post_type' => 'all_products',
    'orderby' => 'menu_order'
);

$child_query = new WP_Query( $args );

There are some HTML here, and after that, when I need the number of children pages, I have this

<?php while ( $child_query->have_posts() ) : $child_query->the_post(); ?>
<?php
    $children = get_pages( array( 'child_of' => $post->ID, 'parent' => 0, 
    'post_type' => 'all_products' ) );
    $children_num =  count( $children );
    echo $children_num;
    ...

After this is executed, I only get 0 If I echo the_permalink(); I get the right link to a child page... Everything seems to be working, only this is the problem

I would appreciate any help or advice. Thanks

  • Looks like the_permalink(); will echo the link for a post_type' ='all_products'. From the docs for get_pages() : This function can also retrieve other post types using the 'post_type' parameter, but the type must be hierarchical like pages, or the function will return false. Also looks like the 'parent' and 'child_of' values are not well, since you are trying to get childs of a given post but hoping they are at the father level, sounds contradictory for me – Pabamato Mar 27 '18 at 1:37
  • Well, I originally tried without parent and post_type, but it didn't work either. But everything works fine if I add 'page' as 'post_type'. I have also added hierarchical to be true in functions.php – Bojan Mar 27 '18 at 10:10
0

EDIT So if i understand correctly what you want is

$children = get_posts( array( 'parent'=> get_the_ID() )); // 'post_type' => 'all_products' in option

Have you tried removing "parent"=>0, meaning that a page has no parent like this

$children = get_pages( array( 'child_of' => $post->ID, 'post_type' => 'svi_proizvodi' ));
  • Unfortunately, this did not work for me :( If it's only 'page' it works fine, but not with custom post type – Bojan Mar 27 '18 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.