[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:
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):
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.
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.
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.
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:
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.