Skip Navigation

[Resolved] Coding Post Relationship (Parent of Child)

This support ticket is created 6 years, 2 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 28 replies, has 3 voices.

Last updated by gabrielB 6 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1115031

Tell us what you are trying to do?

I'm trying to load posts from a many to many relationship. I've got a site with 2 cpts, courses and locations.

Is there any documentation that you are following?

I wonder if there is an update for this post https://toolset.com/forums/topic/coding-post-relationship-parent-of-child/. I used this before the update and it worked perfectly.

What is the link to your site?

hidden link I'll have to create a suer for you though.

#1115560

Hello,

In the latest version of Types plugin, we have introduced new post type relationships, for the new relationship, you will need to try new API function toolset_get_related_posts(), see our document:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
Query related posts by a set of conditions.

#1115619

OK, thanks for that. I still need some guidance, if you can. Please.

So this is a new website I'm working on, I'm just using old code. I don't know if you'll be able to help me further but I'm going to show you what I have and maybe you can guide me on how to change it to what it needs to be.

Old Code
What I'm doing in the below code is displaying the title of a CPT (Agency) that is related to another CTP (Project) with an intermediary CPT (Execution). What I'd like to do is be able to display more than one related posts and if at all possible choose the custom taxonomy as well. So for example I'd want to display the title of a CPT (Agency) that has a custom taxonomy "Cities" and is categorised as Toronto.

<?php  
    $childargs = array(
    'post_type' => 'excecution',
    'posts_per_page' => 1,
    'orderby' => 'title',
    'order' => 'ASC',
    'meta_query' => array(array('key' => '_wpcf_belongs_project_id', 'value' => get_the_ID()))
    );
    $child_posts = get_posts($childargs);

    $post_check = new WP_Query( $childargs );

    if ( $post_check->have_posts() ) { ?>

        <?php foreach ($child_posts as $child_post) {

            $agency_id = wpcf_pr_post_get_belongs($child_post->ID, 'agency');
            $agency = get_post($agency_id);
            $permalink = get_permalink( $agency_id ); 
            $title = get_the_title( $agency_id );
            $excerpt = $agency->post_excerpt;
            ?>

            <?php echo wp_trim_words( $title, 18 ); ?>

        <?php } ?>

    <?php } ?>

<?php wp_reset_postdata(); ?>
#1115638

This is a custom codes problem, if you need more assistance, please provide a test site with the same problem, also point out the problem page URL, and where I can edit your PHP coded, I need to test and debug it in a live website. thanks

#1115640

Deleted this and included in the private message.

#1115655

I have tried the credentials you provided above, it is not valid, I get this error message, please check it:
ERROR: The username or password you entered is incorrect.

You can update your private message here:
https://toolset.com/forums/topic/coding-post-relationship-parent-of-child-2/#post-1115639

#1115656

I'm really sorry about that. My mistake. I still had the User page open and hadn't saved. Please try again.

#1116038

I assume we are talking about the PHP codes in your theme file "course-location.php", can you confirm it?

Please check here:
hidden link

There isn't only a many-to-many relationship "Courses Locations", there isn't the relationship " CPT (Agency) that is related to another CTP (Project)",

And here:
hidden link
There isn't the taxonomy "Cities", where can I find them?

#1116189

Hi Luo, sorry for all the confusion.

Yes, course-location.php is the file I need help with.

When I explained originally, I was using old code from a different website and I didn't know you were actually going to try and fix my code. I'm working on a website for an academy. I've amended my original paragraph to my current situation, below.

What I'm doing is trying to display the title of a CPT (Location) that is related to another CTP (Course) with an intermediary CPT (Course Location). What I'd like to do is be able to display more than one related posts and if at all possible choose the custom taxonomy as well.

Forget about the custom taxonomy, that was a mistake.

Do I need the intermediary CPT?

Thanks so much.

Gabriel

#1116232

I have changed the PHP codes as below:

<?php  

	$course_id = get_the_ID();
    $related_locations = toolset_get_related_posts(
		$course_id, // get posts related to this one
		'course-location', // relationship between the posts
		'parent', // get posts where $writer is the parent in given relationship
		$posts_per_page = 10, $current_page =1, // pagination
		array(), // How was his surname, again…?
		'post_object'
	);

	foreach ($related_locations as $location) {
			$location_id = $location->ID;
            $permalink = get_the_permalink( $location_id ); 
            $title = get_the_title( $location_id ); 
			echo $title; 
			echo $permalink; 
	} 
?>

Please test it again, it is only a demo for your reference,

And the taxonomy "Course Types" is register to parent post type "Course", you can use worpdress function has_term to check if the "Course" post has specific term, then display what you want:
https://developer.wordpress.org/reference/functions/has_term/

#1116255

Luo you are a scholar and a gentleman. Thank you so much!

If I may be so bold as to ask you one last thing. I'm trying to also load a custom field containing an embed code for a map of the location. I've updated the code, but it doesn't seem to be working.

Could you take a look at it please?

#1116473

Actually, I have 2 related locations. Only one is showing up.

#1116863

Thanks for the details, in the Types function types_render_field(), you can specific the "Location" post ID with "post_id" parameter, for example:

$map = types_render_field("google-map-embed-code", array("output"=>"html", "post_id"=>$location_id));

And test again

#1118161

Thank you so much Luo! My issue is resolved now.

Thanks again!

#1118178

Hi Luo, I think you might have missed this. Could you take a look please? I needed to show 2 related locations. Only one is showing up.

Please!