Vineet Sharma
Vineet Sharma

Reputation: 817

Get Custom field value from Custom Post Types with a Parent Child Relationship in Wordpress

I have used the Wordpress Types plugin to create two new Custom Post Types. ie. Hotel and Rooms. The Room post type is the child of Hotel. Along with that I have also added some custom fields in both post types. It includes generic details like Hotel Address, Phone Number, Email etc. Under Room post type, I have added room_image, room_price and room_description custom fields.

I am able to display these custom fields on the single hotel page along with room details. But I am stuck at the index page where I have to display a summary of Hotel details along with room price sorted in descending order. Here's the code that I used to display hotel details

<?php 
                        // WP_Query arguments
$args = array (
    'post_type'              => 'hotel',    
    'order'                  => 'DESC',
    'orderby'                => 'date',
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();

?>
<article>
                        <header>
                            <figure><img width="300" height="160" alt="Placeholder" src="<?php echo get_post_meta($query->post->ID,'wpcf-image',true);?>"><div class="fit-a"></div></figure>
                            <h3><a href="<?php echo get_permalink($query->post->ID ); ?>"><?php echo $query->post->post_title;?></a></h3>
<?php   

        // do something

    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();?>

How can I fetch the child room price based on the parent hotel ?

Upvotes: 1

Views: 6987

Answers (2)

Clemorphy
Clemorphy

Reputation: 474

Inside the loop for each Hotel, you can access child posts of your Hotel type by calling the following function included with the Types plugin : types_child_posts('name_of_your_child_CPT')

In your case it should be something like this :

<?php 
// WP_Query arguments
$args = array (
'post_type'              => 'hotel',    
'order'                  => 'DESC',
'orderby'                => 'date',
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();

?>
<article>
    <header>
        <figure><img width="300" height="160" alt="Placeholder" src="<?php echo get_post_meta($query->post->ID,'wpcf-image',true);?>"><div class="fit-a"></div></figure>
        <h3><a href="<?php echo get_permalink($query->post->ID ); ?>"><?php echo $query->post->post_title;?></a></h3>

    <?php
    // are there rooms for current hotel (room is the name of your child Custom Post type)
    $rooms = types_child_posts('room');

    foreach($rooms as $room)
    {
        echo '<div class="room">';
            // here we display the title of a room
            echo '<div class="room_name">'.$room->post_title.'</div>';
            // here we display a custom field (price) of a room
            echo '<div class="room_price">'.array_pop(get_post_custom_values('wpcf-room-price', $room->ID)).'</div>';
        echo '</div>';
    }

    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();?>

I hope this can help you.

Upvotes: 2

Moishy
Moishy

Reputation: 3648

I hope I understood your question correctly

First we need to call the Hotel Query. Then display its image and custom meta. Then within the hotel query - display the room post and meta.

<?php
$parent = array( 'post_type' => 'hotel', 'post_parent' => 0, 'order' => 'DESC', 'orderby' => 'date');
$hotelparent = new WP_Query($parent);
if ( $hotelparent->have_posts() ) : while ($hotelparent->have_posts()) : $hotelparent->the_post();?>
<article>
<header>
<figure><img width="300" height="160" alt="Placeholder" src="<?php echo get_post_meta($hotelparent->ID,'wpcf-image',true);?>"><div class="fit-a"></div></figure>
<h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>
</header>
<?php $child = array('post_type' => 'hotel', 'post_parent' => $post->ID, 'order' => 'DESC', 'hierarchical'=>1);
$rooms = new WP_Query( $child );
if ( $rooms->have_posts() ) : while ($rooms->have_posts()) : $rooms->the_post(); ?>
<?php echo get_post_meta($rooms->ID,'YOUR_META',true);?>
<?php endwhile; endif; ?>
</article>
<?php endwhile; endif; ?>

Upvotes: 0

Related Questions