0

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();
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; 

        };
    };

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.