Skip Navigation

[Resuelto] 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 hace 7 años, 5 meses. 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Este tema contiene 11 respuestas, tiene 2 mensajes.

Última actualización por daveM hace 7 años, 5 meses.

Asistido por: Shane.

Autor
Mensajes
#537757

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.

I visited this URL: This page gave me a lot of info about creating the parent/child relationship, but it only explains how to show them on the front-end using Views. It doesn't say how to access the child post types using a WP php template - https://toolset.com/documentation/toolset-training-course/part-8-one-to-many-relationships-in-toolset/

Can you explain how I would specify the child/parent relationship in my loop? Thanks!

#537825

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Dave,

Thank you for contacting our support forum.

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.

Please let me know if this helps.
Thanks,
Shane

#537849

Hi Shane, thanks for the reply! That definitely helps.

I was hoping to produce a loop of all child posts. This is what I had:

$parentID = wp_get_post_parent_id( $post_ID );
$args = array(
    'post_type' => 'post',
    'post_parent' => $parentID,
);
$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();

Is there a way I could incorporate what you posted into the loop I already have?
Thanks!

#537862

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Dave,

This code is listing out all the child posts for the currently viewed parent correct?

Please let me know.

Thanks,
Shane

#537869

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.

#538553

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Dave,

Thanks for the clarity.

In order for this to work with your present code a few modifications must be made.

Replace this line below.

$args = array(
    'post_type' => 'post',
    'post_parent' => $parentID,
);

With

$args = array(
    'post_type' => 'post',
    'meta_query' => array(
		array(
			'key'     => '_wpcf_belongs_PARENT_id',
			'value'   => $parentID,
			'compare' => '=',
		),
	),
);

Where you will replace the word PARENT in _wpcf_belongs_PARENT_id with the slug of your Parent CPT.

Please try this and let me know if it helps.
Thanks,
Shane

#539174

Hi Shane,

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!

#539193

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Dave,

This code is going on the current parent post template correct?

Try this.

Replace this

$parentID = wp_get_post_parent_id( $post_ID );

With

$parentID = get_post_id( );

This will only give the correct results if the code is being placed in the template file of the parent.

Thanks,
Shane

#539201

Ah, exactly. I just did the same after realizing which parent ID I was trying to get! Perfect! Thanks for all your help! Sorry if I was unclear!

#540703

Hello, I had one additional question related to this: How would I get a feed of WooCommerce products that are also children of my "Artist" post type?

I have the follow arguments using the same type of WP_Query as above:

$args = array(
	'post_type' => 'products',
	'posts_per_page' => 8,
	'meta_query' => array(
	    array(
		'key'     => '_wpcf_belongs_artist_id',
		'value'   => $parentID,
		'compare' => '=',
	    ),
         ),
);

Is this the correct arguments to pull WooCommerce products related to my custom post type? Thanks!

#542536

I just wanted to check in and see if anyone had some tips of displaying child product post types. See above for context and code. Thanks!

#542586

Apologies. I had my WooCommerce product post type slug wrong. It's working now with the same code as above! Thanks!

El foro ‘Types Community Support’ está cerrado y no se permiten nuevos debates ni respuestas.