Skip Navigation

[Resolved] Listing multiple related posts

This thread is resolved. Here is a description of the problem and solution.

Problem: I have two post types connected in a many-to-many relationship. In a View of post type A, I would like to display a list of all the related post type B for each post type A. The Types field shortcode doesn't seem to work.

Solution: You should use a View to display a list of related posts.

Relevant Documentation:
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/
https://toolset.com/documentation/user-guides/digging-into-view-outputs/#vmeta-wpv-loop-parameters

This support ticket is created 5 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 3 voices.

Last updated by gabrielB 5 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1324539

Hi there,

My website, for an art gallery, has two custom post types: artist and exhibition. They are connected via a many-to-many relationship. On a page listing several exhibitions, I know how to list the information of one artist associated with an exhibition. The code to output a first, middle, and last name of an artist looks like this:

[types field='first-name' item='@artist-exhibition-mm-relationship.parent'][/types] 
[types field='middle-name' item='@artist-exhibition-mm-relationship.parent'][/types] 
[types field='last-name' item='@artist-exhibition-mm-relationship.parent'][/types]

For a group exhibition, however, this won't work; I need to list *multiple* artists. Do I need to create a view? How do I go about that?

Thank you for your help!

Saul

#1324599

On a page listing several exhibitions, I know how to list the information of one artist associated with an exhibition.
Hi, yes a View is required to display a list of related posts. We have more information about this available here: https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/
I assume you mean this page is showing a View of Exhibitions. So basically you will create a new View of Artists and add a Post Relationship Query Filter. If you're not able to see the Query Filter panel, scroll up to the top right corner of the View editor screen and click "Screen Options". You can enable the Query Filter panel here.

Add a Post Relationship Query Filter in the Query Filter panel. Choose your many-to-many post relationship in the configurations, and also choose the option where the post is "related to the current post in the loop". This will filter out all the Artists except those related to the current Exhibition in the View of Exhibitions.

Then scroll down to the Loop Output editor and build your list output using the Loop Wizard. You can include the post title, or a post title with link, or any other combination of fields. Let me know if you have questions about this approach.

#1325599

Christian,

Thanks for your response. This works beautifully!

One refinement I'd like to make is to output the names of the artists connected to the exhibition with commas and spaces between them. Right now, they run together like so:

John Doe Jane Smith Bill Brown, ...

I'd like them to display per below:

John Doe, Jane Smith, Bill Brown, ...

I thought I could pass an argument to the views shortcode ("separator"), but I think that's a fiction in my head. I searched the Toolset documentation but couldn't find anything related to this. If I add it the Loop Item in my view, the last artist will also have a comma and space after him/her/them, which is undesirable.

Is any Toolset magic available?

Saul

#1325607

Nevermind! I solved the last bit myself.

1) The "separator" argument to the shortcode is only for types fields.

2) The Loop Wizard has an option for "List with Separators." That provided the comma-separated list.

Woohoo! Thanks again, Christian!

Saul

#1325983

1) The "separator" argument to the shortcode is only for types fields.
Correct, that won't work for Views.

2) The Loop Wizard has an option for "List with Separators." That provided the comma-separated list.
Yes that's true, or you can use the loop item, index, wrap and pad loop parameters to apply different content based on the loop index in any of the other View output styles. More info about that here if you're interested: https://toolset.com/documentation/user-guides/digging-into-view-outputs/#vmeta-wpv-loop-parameters

#1812551

I need to do this with PHP. Could you help, please?!

<?php

    $client_id = get_the_ID();

    $query = new WP_Query( 
        array(
            'post_type' => 'client',
            'numberposts' => -1,
            //new toolset_relationships query argument
            'toolset_relationships' => array(
                'role' => 'parent',
                'related_to' => $client_id,
                'relationship' =>'client-testimonial'
            ),
            //'meta_key' => 'wpcf-client-order',
            //'orderby' => 'meta_value',
            //'order' => 'ASC',
        )
    );

    $related_clients = $query->posts;

    foreach ($related_clients as $client) {
        
    $client_testimonial_id = $client->ID;
    //error_log("client_id is " . $client_testimonial_id);
    $client_post = get_post($client_testimonial_id);
    $post_slug = $client_post->post_name;
    $permalink = get_the_permalink( $client_testimonial_id );
    $client_title = get_the_title( $client_testimonial_id );
    //$excerpt = $client_post->post_excerpt;
    $excerpt = apply_filters('the_content', $client_post->post_excerpt);
    $content = apply_filters('the_content', $client_post->post_content);

    $thumb_id = get_post_thumbnail_id($client_testimonial_id);
    $thumb_url_array = wp_get_attachment_image_src($thumb_id,'full', true);  
    $testimonials_thumb_url = $thumb_url_array[0];

    $url = types_render_field("url", array("output"=>"raw", "post_id"=>$client_testimonial_id));

    $client_standfirst = types_render_field("client-standfirst", array("output"=>"html", "post_id"=>$client_testimonial_id));
    //$client_address = types_render_field("client-address", array("output"=>"html", "post_id"=>$client_testimonial_id));
?>


<?php echo $client_title; ?>, <?php echo $client_title; ?>


<?php wp_reset_postdata(); ?>