Skip Navigation

[Resolved] Post Relationship not loading properly in an archive.

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

Problem:
Client has a many-to-many relationship and is using the toolset_get_related_posts API function to get related posts. It works in one direction (get A posts related to a B post) but not the other (get B posts related to an A post).

Solution:
The arguments were wrongly supplied to toolset_get_related_posts.

In a one-to-many relationship the one is the 'parent' and the many are the 'children'.

In a many-to-many relationship that same convention doesn't really make sense, but is used anyway.

So the post type on the left is the 'parent' and the post type on the right is the 'child', even though they are more like siblings.

You need to specify the 'parent' and 'child' correctly in the function arguments.

Here is a generic example to retrieve related posts, using the parent-child convention even for a M2M relationship:

$children = toolset_get_related_posts(
	$parent_id,
	$relationship_slug,
	array(
		'query_by_role' => 'parent',
		'limit'			=> 999,
		'return'		=> 'post_id',
		'role_to_return'	=> 'child'
	)
);

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

This support ticket is created 6 years 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 8 replies, has 2 voices.

Last updated by gabrielB 6 years ago.

Assisted by: Nigel.

Author
Posts
#1191083

I've got a many-to-many relationship between two CPT: 'Projects' and 'Services'. I'm managing to load the Services onto the Project, but not the other way round.

You can see it working on 'Test Project 9' on hidden link.
You can see it fail to work on hidden link. The projects should load underneath where it says 'LATEST BRANDING & DESIGN PROJECTS'.

This is how I'm loading the Services into Projects:

<?php  
    $project_id = get_the_ID();
    $related_services = toolset_get_related_posts(
        $project_id, // get posts related to this one
        'project-service', // relationship between the posts
        'parent', // get posts where $writer is the parent in given relationship
        100, 0, // pagination
        array(), // How was his surname, again…?
        'post_object',
        'child'   
    ); ?>

<?php foreach ($related_services as $service) {
        
        $service_id = $service->ID;
        $service_post = get_post($service_id);
        $post_slug = $service_post->post_name;
        $permalink = get_the_permalink( $service_id );
        $title = get_the_title( $service_id );
        //$excerpt = $service_post->post_excerpt;
        $excerpt = apply_filters('the_content', $service_post->post_excerpt);
        $content = apply_filters('the_content', $service_post->post_content);

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

        //$map = types_render_field("google-map-embed-code", array("output"=>"html", "post_id"=>$service_id));
        //$service_address = types_render_field("service-address", array("output"=>"html", "post_id"=>$service_id));
?>

    <h4><?php echo $title; ?></h4>

<?php } ?>

This is how i'm loading the Projects into Services (this isn't working):

<section id="service-projects" class="grid-container full inner-spaced-top">

    <div class="generic-grid">

        <div class="grid-x grid-padding-x archive-grid generic-grid-inner">

            <?php  
                $service_id = get_the_ID();
                $related_projects = toolset_get_related_posts(
                    $service_id, // get posts related to this one
                    'project-service', // relationship between the posts
                    'parent', // get posts where $writer is the parent in given relationship
                    100, 0, // pagination
                    array(), // How was his surname, again…?
                    'post_object',
                    'child'   
                ); ?>
            
            <div class="cell">
                <h4 class="section-title">Latest <?php the_title(); ?> Projects</h4>
            </div>

            <?php foreach ($related_projects as $project) {

                    $project_id = $project->ID;
                    $project_post = get_post($project_id);
                    $permalink = get_the_permalink( $project_id );
                    $title = get_the_title( $project_id );
                    //$excerpt = $learning_partner_post->post_excerpt;
                    $excerpt = apply_filters('the_content', $project_post->post_excerpt);
                    $content = apply_filters('the_content', $project_post->post_content);

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

                    //Get the content, apply filters and execute shortcodes
                    $content = apply_filters( 'the_content', $post->post_content );
                    $embeds = get_media_embedded_in_content( $content ); 

                    // Adjust the amount of rows in the grid
                    $grid_columns = 2;

                    // $map = types_render_field("google-map-embed-code", array("output"=>"html", "post_id"=>$project_id));
                    // $project_address = types_render_field("project-address", array("output"=>"html", "post_id"=>$project_id));
            ?>

                <!--Item: -->
                <article id="post-<?php echo $project_id; ?>" <?php post_class('item cell medium-6 xlarge-6'); ?> role="article">

                    <ul class="cs-style-1">

                        <li class="item-inner show-for-large">

                            <a href="<?php echo $permalink; ?>" title="<?php echo $title; ?>">

                                <figure>

                                    <div class="background-image aspect-ratio" style="background-image: url('<?php echo $thumb_url; ?>');">
                                       &nbsp;
                                    </div>

                                    <figcaption class=""> <!-- Displays on Hover -->
                                        <h3><?php echo $title; ?></h3>
                                        <?php echo '<p>' . wp_trim_words( $standfirst, 8, null ) . '</p>'; ?>
                                        <span class="read-more">Learn More</span>
                                    </figcaption>

                                </figure>

                            </a>

                        </li>

                        <li class="card hide-for-large">

                            <a href="<?php echo $permalink; ?>" title="<?php echo $title; ?>">

                                <div class="featured-image-container" itemprop="articleBody">
                                    <div class="featured-image aspect-ratio" style="background-image: url('<?php echo $thumb_url; ?>');"> </div>
                                </div>

                                <div class="card-section">
                                    <h3><?php echo $title; ?></h3>
                                    <?php echo '<p>' . wp_trim_words( $standfirst, 8, null ) . '</p>'; ?>
                                    <span class="read-more">Learn More</span>
                                </div>

                            </a>

                        </li>

                    </ul>  

                </article> <!-- end article -->

            <?php } ?>

        </div>  <!--End Row: -->

    </div>

</section> <!-- end section -->
#1191085

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Gabriel

In a one-to-many relationship the one is the 'parent' and the many are the 'children'.

In a many-to-many relationship that same convention doesn't really make sense, but is used anyway.

So the post type on the left is the 'parent' and the post type on the right is the 'child', even though they are more like siblings.

In your code examples, when you use the toolset_get_related_posts API function you specify that the starting point in both cases is the parent, which cannot be.

It seems that projects are the 'parent' and services the 'child'.

So in your second example you need to swap around the parent and child argument values, i.e in this case you are starting from the 'child' service and want to retrieve the 'parent' project.

#1191086

Nigel, you're awesome! Thank you so much!

#1191149

Actually I ran into a problem.

Ok so what you told me to do worked like a charm. But I'm complicating it a little here.

If you check hidden link you'll notice that I'm loading the client and the services within the grid.

Now, within Services, I'm loading related Projects hidden link.

What I would like to do is load the clients and services that are related to the Projects while displaying them in Services. I'm not sure I'm explaining myself correctly.

In plain english I want the Projects that are displaying within the Service to have the same data they do when displaying in the Work page.

Help please!

#1191828

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sorry Gabriel,

I'm not quite following.

I see on your site that you have a M2M relationship between clients and projects, and between projects and services.

Your code above let's you get related services when you start from a project, and get related projects when you start from a service.

You could do something similar between clients and projects.

You say " I want the Projects that are displaying within the Service to have the same data they do when displaying in the Work page".

But I'm not sure what they are displaying on the Work page (you have a custom PHP template for it), so I'm not sure what you are trying to display.

#1191845
Screen Shot 2.png
Screen Shot 1.png

Sorry, i know that was confusing.

So in the Services page, I have a Modal for each Service. Within that Modal I'm displaying 2 projects which are related to that service in a grid. The thing is that within that grid, I'm also trying to display 2 relationships (project-client and project-service).

Screen Shot 1: This is the project displaying with relationships. Client is in green at the top and services are in uppercase below the title.

Screen Shot 2: This is the same project displaying in the Service Modal. It's being loaded as a related post and the Client and Services are not loading.

Hope this explains a little better.

#1192027

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, so when you are displaying a project directly, starting with the ID of the project you get the related services and the related client, which you can then output (and this works).

The version which does not work is starting from a service getting two related projects, and then repeating the above, getting the client and services related to that project.

I suspect you are losing track of the post IDs in the second case.

Try adding a few messages to your debug.log to double-check at each stage that you are getting the expected results.

If you haven't already, turn on the debug log by editing your wp-config.php file and change the line with WP_DEBUG like so:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

That will create a debug.log file in your /wp-content/ directory which you can examine in any text editor.

Edit your PHP template code and where you are getting and using post IDs, output them to the debug.log so that you can double-check you have the right ID when required, e.g.

$service_id = get_the_ID();
error_log("service_id is " . $service_id);

A few such messages should help you pinpoint where the code is going wrong.

Let me know what you find.

#1192091

OK, I did that. Here's what I got.

[28-Jan-2019 15:22:56 UTC] service_id is 379
[28-Jan-2019 15:22:56 UTC] service_id is 380
[28-Jan-2019 15:22:56 UTC] service_id is 382
[28-Jan-2019 15:22:56 UTC] service_id is 381

#1192456

My issue is resolved now. Thank you!

I was calling the same variable for both. It was and ID thing, just like you said.

Thanks Nigel!