How to access the parent post from child post
-
Hello
Post Type 1 : ‘disque’ (custom post type)
Belongs to
Post type 2 : ‘artiste’ (custom post type)I retrieve several ‘disque’ and for each one of them, I want to display the ‘artiste’ name (title).
Here is my code :
$args = array( 'post_type' => 'disque', 'numberposts' => '10', 'orderby' => 'Post_date', 'order' => 'DESC', ); $p_query = new WP_Query($args); while ( $p_query->have_posts() ) : $p_query->the_post(); //name of the 'disque' echo get_post_meta($post->ID, 'wpcf-album', true); //Trying to get the parent (= the 'artiste' to which the 'disque' belongs $parent = wp_get_post_parent_id($post->ID);//Also tried with $post $parent_post = get_post($parent); echo $parent_post->post_title; echo $parent_post->post_permalink; echo '<br/>'; endwhile ?>
With this method, I thought I would for each ‘disque’ in the loop retrieve its parent (‘artiste’). But for each ‘disque’, I get the same disque as parent. Don(t know if it’s clear.
I’m quite sure there is an easier way to access a parent from a child (and maybe, in the case of a belongs to relationship we cannot say parent and child).
Well, a bit confused.
Thx for your help
- The topic ‘How to access the parent post from child post’ is closed to new replies.