Sauter la navigation

[Résolu] Intermediary post ID in php

This support ticket is created Il y a 6 années et 5 mois. 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Ce sujet contient 8 réponses, a 2 voix.

Dernière mise à jour par mohammadD Il y a 6 années et 5 mois.

Assisté par: Beda.

Auteur
Publications
#921810

I have a relationship by the slug name student-course , between two post types ' students' and 'courses' . this relationship is Many-to- Many , i have the parent student and child course id in my form and i want to get the intermediary post Id because i have to update a meta value of the relationship.
can you help me how to do it or what relationship api i should use ?

#921939

To get related posts you would use toolset_get_related_posts, which as well supports an argument to return the intermediate Post Object hence you could get your data from there:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

However, it depends on the data you feed the above API function, if it works or not.
On a Form that creates new posts for example, you need to ensure you hook in the CRED API late enough (cred_save_data or later or save_post with 20 or above priority).
Otherwise the data is not yet ready, you will not receive anything in the above API call and the code fails.

An example of the above API Function is included in the linked DOC, while the CRED API for Toolset Forms to use is documented here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Let me know if you need more help with

#921977

Thanks beda , yeah i know but i cant do it with the tests i have done.can you create a sample code by get related posts api which you have the child and parent id and then result will be intermediary post id in a many to many relationship?

#922015
Bildschirmfoto 2018-07-07 um 17.28.43.png

Sure.

1. Create 2 Post types as shown in the ScreenShot, in a M2M Relation (Left Type, Right Type I called them)
2. Create at least one Post each Type and connect them
3. Apply this code to get the intermediary Post ID of the relation between above 2 posts:

$query_by_element = 27; //Our Parent (Left type), edit as required
			$relationship = 'left-type-right-type'; // edit as required
			$query_by_role_name = 'parent'; //$query_by_element is a child in this relation, edit as required 
			$limit = 100; //defaults
			$offset = 0; //defaults
			$args = array(); //nothing needed
			$return = 'post_id'; //We want Post ID
			$role_name_to_return = 'intermediary'; //We want Inetermediary
			$out_test = toolset_get_related_posts(
							$query_by_element,
							$relationship,
							$query_by_role_name,
							$limit,
							$offset,
							$args,
							$return,
							$role_name_to_return
						);
			var_dump($out_test);

That returns the Intermediary Post ID (in the variable dump in my example, use as required).

This is crafted with a many to many relationship in mind that is not migrated, but created on a fresh install

#922029

really thanks beda for the documented code but i am afraid i think there is a problem in the code you have sent. by this code you will get all 100 intermediary posts who is connected to the left type. but in my question i need the intermediary post id between left type post by id 'x' and right type post by id 'y'. i don't see in your code you specified the right type post.
can you answer this question with a block of code please ?

#922200

I found a way to do it , if you are agree that this is the only way to do so , let this ticket has a quick result so others can use.

  $s=$_POST['students']; // id of the parent
    $query_by_element = $s; //Our Parent (Left type), edit as required
    $relationship = 'student-course'; // edit as required
    $query_by_role_name = 'parent'; //$query_by_element is a child in this relation, edit as required 
    $limit = 100; //defaults
    $offset = 0; //defaults
    $args = array(); //nothing needed
    $return = 'post_id'; //We want Post ID
    $role_name_to_return = 'intermediary'; //We want Inetermediary
    $out_test = toolset_get_related_posts(
    $query_by_element,
    $relationship,
    $query_by_role_name,
    $limit,
    $offset,
    $args,
    $return,
    $role_name_to_return
    );
    $query_by_element = $_POST['courseid']; //Our child(right type), edit as required
    $relationship = 'student-course'; // edit as required
    $query_by_role_name = 'child'; //$query_by_element is a child in this relation, edit as required 
    $limit = 100; //defaults
    $offset = 0; //defaults
    $args = array(); //nothing needed
    $return = 'post_id'; //We want Post ID
    $role_name_to_return = 'intermediary'; //We want Inetermediary
    $out_test2 = toolset_get_related_posts(
    $query_by_element,
    $relationship,
    $query_by_role_name,
    $limit,
    $offset,
    $args,
    $return,
    $role_name_to_return
    );
    $result=array_intersect($out_test,$out_test2);// get the intermediary id by intersecting both results.
#922823

Yes, that is what I was going to propose too.

Here the snippet I crafted (sorry I was on my weekend):

$query_by_element = 5; //Our Parent (Left type), edit as required
            $relationship = 'left-type-right-type'; // edit as required
            $query_by_role_name = 'parent'; //$query_by_element is a child in this relation, edit as required 
            $limit = 100; //defaults
            $offset = 0; //defaults
            $args = array(); //nothing needed
            $return = 'post_id'; //We want Post ID
            $role_name_to_return = 'intermediary'; //We want Inetermediary
            $out_parent = toolset_get_related_posts(
                            $query_by_element,
                            $relationship,
                            $query_by_role_name,
                            $limit,
                            $offset,
                            $args,
                            $return,
                            $role_name_to_return
                        );

            $query_by_element = 7; //Our Parent (Left type), edit as required
            $relationship = 'left-type-right-type'; // edit as required
            $query_by_role_name = 'child'; //$query_by_element is a child in this relation, edit as required 
            $limit = 100; //defaults
            $offset = 0; //defaults
            $args = array(); //nothing needed
            $return = 'post_id'; //We want Post ID
            $role_name_to_return = 'intermediary'; //We want Inetermediary
            $out_child = toolset_get_related_posts(
                            $query_by_element,
                            $relationship,
                            $query_by_role_name,
                            $limit,
                            $offset,
                            $args,
                            $return,
                            $role_name_to_return
                        );
            $out = array_intersect($out_child,$out_parent);// get the intermediary id by intersecting both results.
            var_dump($out);

This code basically does the same operation twice then compares the output and spits out the only possible match.

The problem I see and the reason why I will ask for a feature here, is that it's cumbersome and runs a query twice while we could just offer an API to get_ipo_object/id

I will suggest that to the developers.

#923363

I have good news, there will eventually be in future a new, shorter way to do that.
Eventually we will add a API function to get IPO's directly.

Please keep an eye on the updates and blog posts 🙂

#923799

That would be greate. your support is awsome.