Skip Navigation

[Resolved] cannot read the value of a relatioship in php.

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 6 replies, has 2 voices.

Last updated by Waqar 5 years, 3 months ago.

Assisted by: Waqar.

Author
Posts
#1170618

I have a Gred Form with a listbox of a relationship one to many and
1) I want to pass that value to a php funtion
2) to find all the children post of that parent

I have read the post
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

and my relationship slug is 'target-collection-target'
so i used the suggested command in order to get the id of the parent

$tid = toolset_get_related_post( $post_id, 'target-collection-target','parent' );

and then the command

$tname = get_the_title($tid);

to read the actual value of that list box in cred form

THIS DOES NOT WORK

b) as it is presented in the post-relationships-api
if I want to get all the names of the childer of a parent i have to use the following code

$books = toolset_get_related_posts(
$writer, // get posts related to this one
array( 'writer', 'book' ), // relationship between the posts
'parent', // get posts where $writer is the parent in given relationship
$posts_per_page, $current_page, // pagination
array( 's' => 'Harry' ), // How was his surname, again…?
'post_object'
);

in my previous toolset version, I used that code
$result = get_posts(array(
'post_type' => 'target',
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page'=> -1,
'numberposts' => -1,
'meta_query' => array(
array(
'key' => '_wpcf_belongs_target-collection_id',
'value' => $tid,
'compare' => '='
)
)
));

but i cannot understand how to convert to your suggested one

thanks in advanced

#1171142

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Kostas,

Thank you for contacting us and I'll be happy to assist.

As explained in the documentation for the "toolset_get_related_posts" function, all the parameters are needed in the correct order.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Following example will make this more clear:


$query_by_element = get_the_ID(); // ID of post to get relationship from
$relationship = 'target-collection-target'; // relationship slug
$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation 
$limit = 100; // defaults
$offset = 0; // defaults
$args = array(); //nothing needed
$return = 'post_id'; // We want Post ID
$role_name_to_return = 'child'; // We want children.
 
$get_results = toolset_get_related_posts(
                $query_by_element,
                $relationship,
                $query_by_role_name,
                $limit,
                $offset,
                $args,
                $return,
                $role_name_to_return
                );
 
print_r($get_results);

Once, you've got the "toolset_get_related_posts" function working as expected, you can use it inside a custom shortcode, to pass on values in your form.

I hope this helps.

regards,
Waqar

#1174352

Thanks for the reply,

If I have understood well you answered the part B of my question on how to retrieve all the children of a parent.

but the first part :" how to get the parent post of the current post" still is something that i am trying to run.

up to now i have the following code to read the parent of a current post type

I USE THE NEW RELATIONSHIP not LEGACY

add_action('save_post', 'get_the_parent',19,1);

function get_the_parent($post_id);
{
$album= get_post($post_id);
$song= toolset_get_related_post( $product, 'song-collection-song', 'parent');
}

up to now is this code provide my the parent of the current post as i have select in my Cred Form ?

#1175087

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Kostas,

As mentioned in the documentation ( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post ) for complex cases (i.e. many-to-many relationships), "toolset_get_related_posts" function should be used, instead of "toolset_get_related_post" function.

That function can be used for both cases, whether you'd like to get the parent relative of current post (1) or if you'd like to get all child relatives of that parent (2).

I'll recommend updating your code to use "toolset_get_related_posts" function, as shown in the example code in my last reply.

In case it still doesn't work, I'll need access to the admin area to see how the relationship and the CRED form is set, to troubleshoot this further.

You're welcome to share temporary admin user login details, along with the details on how and where you expected the code to work.

I've set your next reply as private so that only you and our support team will have access to it.

Important note: Please make a complete backup copy of your website, before sharing the access details.

regards and Happy New Year,
Waqar

#1175820

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Kostas,

Thanks for sharing the admin area access details.

I've checked the code in your plugin's file and noticed that the function name that is being called with hooks, doesn't match the one defined for the code, which means that the code is never getting executed.
( screenshot: hidden link )

Also, the custom function doesn't restrict the code execution for a specific post type or a CRED form, which means it will be running unnecessarily resulting in a potential performance issue.

The hooks "save_post" and "cred_save_data" expect functions with a different number of parameters, therefore, you'll need to create two different functions for the admin area editing ("save_post") and the CRED form ("cred_save_data").

Example code with "save_post" hook:


function auto_get_children_from_parent( $post_id ) {
	// if a specific post type
	if ( get_post_type( $post_id ) == 'target' ) {
		
		// get parent post from the relationship
		$query_by_element = get_the_ID(); // ID of post to get relationship from
		$relationship = 'target-collection-target'; // relationship slug
		$query_by_role_name = 'child'; // $query_by_element is a child in this relation 
		$limit = 1; // defaults
		$offset = 0; // defaults
		$args = array(); //nothing needed
		$return = 'post_id'; // We want Post ID
		$role_name_to_return = 'parent'; // We want parent.

		$get_parent_result = toolset_get_related_posts(
			$query_by_element,
			$relationship,
			$query_by_role_name,
			$limit,
			$offset,
			$args,
			$return,
			$role_name_to_return
		);

		if(!empty($get_parent_result[0]))
		{
			$parent_id = $get_parent_result[0];

			// get child posts from the relationship
			$query_by_element = $parent_id; // ID of post to get relationship from
			$relationship = 'target-collection-target'; // relationship slug
			$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation 
			$limit = 100; // defaults
			$offset = 0; // defaults
			$args = array(); //nothing needed
			$return = 'post_id'; // We want Post ID
			$role_name_to_return = 'child'; // We want children.

			$get_child_results = toolset_get_related_posts(
				$query_by_element,
				$relationship,
				$query_by_role_name,
				$limit,
				$offset,
				$args,
				$return,
				$role_name_to_return
			);
		}
	}
}

add_action( 'save_post', 'auto_get_children_from_parent', 99 );

Example code with "cred_save_data" hook:


function auto_get_children_from_parent_form( $post_id, $form_data ) {
	// if a specific form
	if ($form_data['id']==123) {
		
		// get parent post from the relationship
		$query_by_element = $post_id; // ID of post to get relationship from
		$relationship = 'target-collection-target'; // relationship slug
		$query_by_role_name = 'child'; // $query_by_element is a child in this relation 
		$limit = 1; // defaults
		$offset = 0; // defaults
		$args = array(); //nothing needed
		$return = 'post_id'; // We want Post ID
		$role_name_to_return = 'parent'; // We want parent.

		$get_parent_result = toolset_get_related_posts(
			$query_by_element,
			$relationship,
			$query_by_role_name,
			$limit,
			$offset,
			$args,
			$return,
			$role_name_to_return
		);

		if(!empty($get_parent_result[0]))
		{
			$parent_id = $get_parent_result[0];

			// get child posts from the relationship
			$query_by_element = $parent_id; // ID of post to get relationship from
			$relationship = 'target-collection-target'; // relationship slug
			$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation 
			$limit = 100; // defaults
			$offset = 0; // defaults
			$args = array(); //nothing needed
			$return = 'post_id'; // We want Post ID
			$role_name_to_return = 'child'; // We want children.
			
			$get_child_results = toolset_get_related_posts(
				$query_by_element,
				$relationship,
				$query_by_role_name,
				$limit,
				$offset,
				$args,
				$return,
				$role_name_to_return
			);
		}
	}
}

add_action('cred_save_data', 'auto_get_children_from_parent_form', 10, 2);

Note: Please replace "123" with the actual CRED form ID, for which you'd like to execute this code.

For both functions, you'll get the IDs of all child posts of the parent, in relation with the current post, into the array "$get_child_results".

Tip: To debug the custom code step-by-step, you'll find this guide useful:
hidden link

regards,
Waqar

#1176266

Hi,
thanks for the help here :),
regarding the function's name it was indeed wrong by accident ..
I did try your code only for the save post for now but when I am going to create a new post i get a white blank page.
I looked for error in debug file but found non....

it is the same login if you need to

#1176799

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Kostas,

Thanks for sharing the update.

Whenever some code breaks and a blank screen shows, some errors or warnings are recording in the server's error log. If you don't see any errors/warnings, you'll need to turn on the debugging, first:

https://codex.wordpress.org/Debugging_in_WordPress
hidden link

I also noticed that your plugin file includes the additional code and not just what I've shared in my last message. As much as we would like to help, the support that we provide is limited to Toolset related features, APIs and functions only. You can read the details around what is covered at our support policy page:
https://toolset.com/toolset-support-policy/

To keep things simple, you can first limit the plugin's code to only getting the desired post IDs. When that part is working, you can then keep on adding additional bits, as needed. This way it will be clear, what is working and what is not.

For more personalized assistance around custom programming, you can also consider hiring a professional from our list of contractors:
https://toolset.com/contractors/

regards,
Waqar

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.