[Résolu] How would I create a feed of child posts only for a specific parent?
The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created Il y a 7 années et 5 mois. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.
I am trying to: I have created a custom post type, "artist", that I've made a parent of the default WP "post" type. I would like to create a feed in a php template that only shows 'post's that are children of a certain 'artist' post. I'm not using Views, so I have a WP_Query loop, but I'm not sure how to specify this parent/child relationship.
Actually the snippet of code you need to focus on is this right here.
//It will query all child posts of the current event, that are appearance type
$child_posts = types_child_posts('appearance');
foreach ($child_posts as $child_post) {
$band_id = wpcf_pr_post_get_belongs($child_post->ID, 'band');
//You can also use WP Native API get_post_meta to get the parent post ID
//as it is stored in a hidden custom field _wpcf_belongs_post-type-slug_id
//$band_id = get_post_meta($child_post->ID, '_wpcf_belongs_band_id', true);
$band = get_post($band_id);
echo $band->post_title;
}
Where you will need to replace appearance with the slug of your parent cpt. Add that to your Parent Post type template and it will display the child posts.
Well, the code I just posted seems to list out all posts regardless of parent. I need to limit it to only show children of the current post. Is that possible with the code I posted?
I'd like to use the loop I posted because I already have all of the post content that I want set.
Thanks for the reply! I switched out that code and replaced PARENT with the slug of my parent CPT, but now it's not showing any posts at all. The 'if' statement fails and my 'else' fires: "There is no news..."
This is my new code:
$parentID = wp_get_post_parent_id( $post_ID );
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => '_wpcf_belongs_artist_id',
'value' => $parentID,
'compare' => '=',
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<ul class="artist">';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . the_post_thumbnail();
echo '<div class="artist-news-text"><p class="artist-news-date">' . get_the_date() . '</p>';
echo '<h2>' . get_the_title() . '</h2>';
echo '<p class="artist-news-excerpt">' . get_the_excerpt() . '</p>';
echo '<a class="artist-news-read-more" href="' . get_permalink( get_the_ID() ) . '">Read More</a></div>';
echo '</li>';
}
echo '</ul>';
} else {
/* show 404 error here */
echo '<p class="artist-no-news">There is no news for this artist at this time.</p>';
} wp_reset_postdata();
It seems like this should work. Any idea why it's not?
Thanks for your help!