Search code examples
wordpresscustom-post-typehierarchy

Wordpress - Trying to retrieve only immediate children of custom post in loop


I have a custom post type called 'location' where the top level is state, second level down is city, and the third level is the actual location.

I've used the methods recommended here and here with no luck. I've tried get_pages(), get_posts(), get_children() and it's either retrieving all children or retrieving all pages.

Here is my current code that is in the single template of the custom post type:

<?php $args = array(
                'child_of' => $post->ID, 
                'parent ' => $post->ID,
                'hierarchical' => 0,
                'sort_column' => 'menu_order', 
                'sort_order' => 'asc',
            ); 
$locations = get_pages($args); ?>
    <?php
        foreach ($locations as $location):
            $title  = get_the_title($location->ID);
            $locale = get_field('location',$location->ID);
            $lat    = $locale['lat'];
            $lng    = $locale['lng'];
            $addy   = $locale['address'];
            $hours  = get_field('hours',$location->ID);
            $link   = get_the_permalink($location->ID);
    ?>
                <div class="location-single" data-lat="<?php echo $lat; ?>" data-lng="<?php echo $lng; ?>">
                    <?php echo $title; ?>
                    <address><?php echo $addy; ?></address>
                    <time><?php echo $hours; ?></time>
                </div>
    <?php endforeach; ?>

EDIT: To cover all my bases, here is the code i'm using to register this custom post type.

function custom_post_type() {

$labels = array(
    'name'                  => _x( 'Locations', 'Post Type General Name', 'text_domain' ),
    'singular_name'         => _x( 'Location', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'             => __( 'Locations', 'text_domain' ),
    'name_admin_bar'        => __( 'Location', 'text_domain' ),
    'archives'              => __( 'Locations Archives', 'text_domain' ),
    'attributes'            => __( 'Location Attributes', 'text_domain' ),
    'parent_item_colon'     => __( 'Parent Location:', 'text_domain' ),
    'all_items'             => __( 'All Location', 'text_domain' ),
    'add_new_item'          => __( 'Add New Location', 'text_domain' ),
    'add_new'               => __( 'Add New', 'text_domain' ),
    'new_item'              => __( 'New Location', 'text_domain' ),
    'edit_item'             => __( 'Edit Item', 'text_domain' ),
    'update_item'           => __( 'Update Item', 'text_domain' ),
    'view_item'             => __( 'View Item', 'text_domain' ),
    'view_items'            => __( 'View Items', 'text_domain' ),
    'search_items'          => __( 'Search Item', 'text_domain' ),
    'not_found'             => __( 'Not found', 'text_domain' ),
    'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
    'featured_image'        => __( 'Featured Image', 'text_domain' ),
    'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
    'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
    'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
    'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
    'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
    'items_list'            => __( 'Items list', 'text_domain' ),
    'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
    'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
);
$args = array(
    'label'                 => __( 'Location', 'text_domain' ),
    'description'           => __( 'Locations', 'text_domain' ),
    'labels'                => $labels,
    'supports'              => array( 'title', 'editor','page-attributes' ),
    'hierarchical'          => true,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_icon'             => 'dashicons-location',
    'menu_position'         => 8,
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => true,
    'exclude_from_search'   => false,
    'publicly_queryable'    => true,
    'capability_type'       => 'page',
);
register_post_type( 'location', $args );

}
add_action( 'init', 'custom_post_type', 0 );

I'm inserting this into the the_content area using this:

add_filter('the_content', 'location_content');  

function location_content( $content ) {
    if ( is_singular( 'location' ) ) {
        include dirname( __FILE__ ) . '/templates/location.php'; 
    }
    else {
        return $content;
    }
}

Could this be the issue? Would it work better if I had a single template with this code on it instead?

EDIT: I've tried including this code and also the solution below in the single template file directly and it still returns nothing.


Solution

  • Found the answer for my usecase:

    Instead of get_pages or get_posts I'm using new WP_Query. that solved all my problems. Thank you everyone for helping.