最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom post types - Attempting to get number of grandchildren of page in WP_Query loop

matteradmin10PV0评论

I have a custom archive page(with map and location data) for a custom post type. Due to how the locations are hierarchal(state, city, location), I want to show the states only on the archive page, and a count of the grand-children on the marker(using google maps API). The marker with text is working great, as is grabbing only the top level locations. The issue I'm having is getting an accurate count.

This is my current code. I've tried get_posts, get_pages, as well as WP_query. It's returning the postID in the actual sub-queries as '0' and is returning all pages as the count. Am I missing something super obvious?

$query = new WP_Query(array(
    'post_type' => 'location',
    'posts_per_page' => -1,
    'post_parent' => 0,
    'orderby' => 'title',
    'order'   => 'ASC',
));
$locations = '';
while ($query->have_posts()) {
    $query->the_post();

    $post_id        = $post->ID;
    $title          = get_the_title($post_id);
    $permalink      = get_the_permalink($post_id);
    $locationdata   = get_field('location',$post_id);
    $latitude       = $locationdata['lat'];
    $longitude      = $locationdata['lng'];
    $address        = $locationdata['address'];
    $hours          = get_field('hours',$post_id);

    $counter = new WP_Query(array( 'post_parent' => $query->$post->ID, 'post_type' => 'location', 'posts_per_page' => -1 ) );    

    $countchildren = $counter->post_count; 

    $mapslocations = $mapslocations.'[new google.maps.LatLng('.$latitude.', '.$longitude.'), \''.$title.'\', \'<address>'.$address.'</address>\',"'. $permalink .'","'.$countchildren.'"],';

    $locations = $locations.'<li><h2><a href="'.get_the_permalink().'">'.get_the_title().'</a></li>';
}
wp_reset_postdata();

I have a custom archive page(with map and location data) for a custom post type. Due to how the locations are hierarchal(state, city, location), I want to show the states only on the archive page, and a count of the grand-children on the marker(using google maps API). The marker with text is working great, as is grabbing only the top level locations. The issue I'm having is getting an accurate count.

This is my current code. I've tried get_posts, get_pages, as well as WP_query. It's returning the postID in the actual sub-queries as '0' and is returning all pages as the count. Am I missing something super obvious?

$query = new WP_Query(array(
    'post_type' => 'location',
    'posts_per_page' => -1,
    'post_parent' => 0,
    'orderby' => 'title',
    'order'   => 'ASC',
));
$locations = '';
while ($query->have_posts()) {
    $query->the_post();

    $post_id        = $post->ID;
    $title          = get_the_title($post_id);
    $permalink      = get_the_permalink($post_id);
    $locationdata   = get_field('location',$post_id);
    $latitude       = $locationdata['lat'];
    $longitude      = $locationdata['lng'];
    $address        = $locationdata['address'];
    $hours          = get_field('hours',$post_id);

    $counter = new WP_Query(array( 'post_parent' => $query->$post->ID, 'post_type' => 'location', 'posts_per_page' => -1 ) );    

    $countchildren = $counter->post_count; 

    $mapslocations = $mapslocations.'[new google.maps.LatLng('.$latitude.', '.$longitude.'), \''.$title.'\', \'<address>'.$address.'</address>\',"'. $permalink .'","'.$countchildren.'"],';

    $locations = $locations.'<li><h2><a href="'.get_the_permalink().'">'.get_the_title().'</a></li>';
}
wp_reset_postdata();
Share Improve this question edited Mar 29, 2019 at 2:35 Eje 1654 bronze badges asked Mar 21, 2019 at 16:31 BenlovBenlov 215 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Found a solution!

$inner_query = new WP_Query(array( 'post_parent' => $parentid, 'post_type' => 'location', 'posts_per_page' => -1 ) ); 

    $countchildren = $inner_query->post_count; 
    $counterz = 0;
    if ( $inner_query->have_posts() ) {
       while ( $inner_query->have_posts() ) {
          $inner_query->the_post();

           $posterid = get_the_ID(); 
           $inner_inner_query = new WP_Query(array( 'post_parent' => $posterid, 'post_type' => 'location', 'posts_per_page' => -1 ) ); 

           $counterz = $counterz += $inner_inner_query->post_count; 

        };
    };
Post a comment

comment list (0)

  1. No comments so far