Skip Navigation

[Resolved] you have provide me a code for toolset_get_related_posts but does not work

This support ticket is created 5 years, 3 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
- 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+01:00)

This topic contains 13 replies, has 2 voices.

Last updated by kostasO 5 years, 3 months ago.

Assisted by: Nigel.

Author
Posts
#1183507

I am trying to: read the relationship value of a list box as i have posted here:
https://toolset.com/forums/topic/cannot-read-the-value-of-a-relatioship-in-php/

after many days of trying to find what is wrong, herring 3 freelancer to find the error i just copied just your code and i still get the white screen of death when i am trying to post a new custom post

#1183579

Nigel
Supporter

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

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

Hi Kostas

I looked at the earlier thread and saw you exchanged quite a few messages with Waqar.

Looking at your initial question, I'm not entirely sure I understand what you need.

You have a one-to-many relationship (between which two post types?).

You have a Toolset Form with a "listbox of a relationship".

I assume, therefore, that the form is to publish child posts, and the select dropdown is to choose which parent the child will belong to, is that correct?

So, when the form is submitted, a child post will be published, and it will "belong" to a parent post.

If you use a hook such as cred_save_data then you will have the ID of both the child post and the parent post available to your code.

What is it that you then want to happen?

#1183613

Hi Nigel,
Thank you for your time here 🙂

it is almost exactly how you described.
When I use the hook I need the parent post that has been selected from the list box and all its children as objects in order to be able to read values from their custom fields and write them to a simple txt file.

I had a plugin that worked perfectly with a previous toolset version. after updating to the latest when i use the toolset_get_related_posts either to get the parrent or to get all its children, i get a white screen and the code stoped working.

I tried lots of thinks to my code for many days but at the end i just made a plugin with only the code that i got from the support and still getting the white screen of death ...

so it is a toolset error that I cannot read by any debug method I tried

#1183650

Nigel
Supporter

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

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

Can I get your site credentials to look at what you have added?

(I checked those from the previous thread but they no longer work.)

#1184037

Hi, did you see it ?

#1184066

Nigel
Supporter

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

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

Hi Kostas

I just tried the credentials now, but the password appears to be wrong, I can't log in.

Could you please try again?

#1184335

Nigel
Supporter

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

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

OK, I'm in (I had to enter via that small arrow).

I'm not sure which form is your child form that the code should be triggered by, so you will need to edit the code I added at Toolset > Settings > Custom Code to update the ID of the form to be targeted.

For reference, here is the code snippet.

function tssupp_targets($post_id, $form_data) {

	if ( in_array($form_data['id'], array( 123 ))) { // Edit the ID of your (child) form


		$relationship = 'target-collection-target'; // Edit slug

		$param = '@' . $relationship . '_parent';

		$parent_id = $_REQUEST[$param];

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

		// Add the newly created child post to the list
		$child_posts[] = $post_id;

		// you now have an array of child post IDs for this parent


	} 
}
add_action('cred_save_data', 'tssupp_targets', 10, 2);

It generates an array of the child post IDs, which you can then use for... whatever it was you were aiming to do next.

#1184926

Hi Nigel,
Thank you for your help.

I just saw your solution and i have the following questions :

0) the $child_posts[] is an array that contains all the child post of the parent (who is the parent post of the specific form that I run the code in the save hook) with the first value on the $child_posts[0] ?

1) in order to be able to use this with many child forms logically If I add some OR parameters to the if statement

if ( in_array($form_data['id'], array( 123 )))

it would work for each separate new custom type that I want to be children of a new parent custom type.
(I want to do with this is to read child post in many post types that I will create...)

2) I can extend the code here as I can when I create a regular wp- plugin in order to do all the functions I want?

3) In order to get the parent before the list of the childer, I should use?

$parrent_post = toolset_get_related_posts($parent_id, $relationship, array(
'query_by_role' => 'child',
'limit' => 999,
'return' => 'post_id',
'role_to_return' => 'parent',
));
and logicaly i will have only one value at $parrent_post[0] that incudes the parrent of my child post that i will run the save hook ?

4) can it work also in the save_data hook?

4) this part of code works only in Custom toolset or can I add it to my plugin?

thank you again for your time

best
kostas

#1184963

I have remake all my code inside the toolset custom code

I run the test button and it returns no error but still does not run...

in very shortly this is a simple code that:
- reads the parent : $parent_id = $_REQUEST[$param];
- reads the children as post_id array $child_posts
- creates a folder with the parent name inside the server
- converts the array of $child_posts from an array of id to an array of post
- using a foreach loop for all $child_posts (as objects) to read the meta fields of them and write a txt file inside the above path)

I have commented all the code and I have entered an OR statement to your IF in order to read two forms. one for new and one for edit post

but still no functionality ...

#1184970

Nigel
Supporter

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

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

Hi Kostas

I can't comment on your own custom code, but I made a few minor changes to the start of the code based upon your comments.

The main problem would be the way you edited the if statement to see whether the code should run, which would not have worked.

Here is the correct way to limit this to work on your two forms, sorry if that was not clear:

function tssupp_targets($post_id, $form_data) {

  	// my form ids are 49 for edit target and 50 for new target
	if ( in_array($form_data['id'], array( 49, 50 ) ) ) 
    {  

		$relationship = 'target-collection-target'; // Edit slug

		$param = '@' . $relationship . '_parent';

		$parent_id = $_REQUEST[$param];

		$child_posts = toolset_get_related_posts($parent_id, $relationship, array(
			'query_by_role' => 'parent',
			'limit' => 999,
			'return' => 'post_object',
			'role_to_return' => 'child',
		));

		// Add the newly created child post to the list
      	$child_posts[] = get_post($post_id);

		// you now have an array of child post objects for this parent
		error_log(print_r($child_posts, true));
...

As well as the if statement, I also changed the 'return' argument of the the toolset_get_related_posts call to 'post_object', so that what is returned is an array of child posts as post objects instead of an array of post ids.

In the final line of this part you can see I'm sending this array of child posts to your debug.log so that you can inspect it and confirm that it has returned what you expect, namely an array of child post objects that are children of the parent post selected in the form.

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.

#1186020

Hi NIgel,
Thank you for your soloution. It works for getting all the children but not the parent.

in order to read the parent post of the current child i use the code

$parent_post = toolset_get_related_posts($parent_id, $relationship, array(
'query_by_role' => 'child',
'limit' => 1,
'return' => 'post_object',
'role_to_return' => 'parent',
));

but in debug.log i get the word "Array() " only. What i am doing wrong here ?

alse=i cannot read the meta valuesof the child posts. to do tha I use a foreach loop

foreach($child_posts as $post)
{
$pid = $post->ID;
$post_user = get_the_author_meta("user_login",$post->post_author);
if ($post_user == $uname)
{
$a1 = get_post_meta($pid,"wpcf-ar-image-target",true);
$link = get_post_meta($pid,"wpcf-link",true);
$ylink = get_post_meta($pid,"wpcf-youtube-link",true);
}
}

thanks

#1186026

managed to solv the read meta problem,

so only the parrent problem is active

thanks

#1186579

Nigel
Supporter

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

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

I don't understand why you are using the API to retrieve the parent post, as you already have the parent post id which is available in the $_REQUEST object.

If you want the parent post object and not just the id then you can get that with get_post:

$parent_post = get_post( $parent_id );

https://developer.wordpress.org/reference/functions/get_post/

#1186588

I used that way of getting the parent because the previous support on the same issue had suggest me to .

in order to have a correct code to others to see in the solution you could change the

$child_posts[] = get_post($post_id);

in order to also put an object to the array and not an id

thank you a lot for your time

🙂

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