Skip Navigation

[Resolved] Empty array when I query related posts using toolset_get_related_posts

This thread is resolved. Here is a description of the problem and solution.

Problem: I am trying to use the toolset_get_related_posts API to display some related posts, but when I echo the results I see "Array". I should see posts.

Solution: The results returned by the API will be an array of post objects. If you try to echo those directly, you will see "Array". You can loop over those objects with a foreach, or you can inspect them using print_r.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
http://php.net/manual/en/language.types.array.php
https://codex.wordpress.org/Debugging_in_WordPress

This support ticket is created 6 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.

Our next available supporter will start replying to tickets in about 1.26 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by HashimW3633 6 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#949164
2array.png
edit page.png

I am trying to: Output a list of post IDs. I have an "event" post type with a many to one relationship with "pages".

I created a new page and associated 2 events with it.

I expected to see: The number of related "events" and a list of the IDs

Instead, I got: "2Array"

Here's the code I'm using in my page.php template

$listChildEvents = toolset_get_related_posts(
get_the_ID(),
'page-events',
'parent',
100,
0,
array(),
'post_id',
'child',
null,
ASC,
true,
$countPosts
);
echo $countPosts; // output is "2"
echo $listChildEvents; // output is "Array"

#949167

Another thing - when I add and remove "events" the $countPosts variable properly outputs the correct number.

#949181
page and evernt relationship.png

I changed it to a many-to-many relationship, and re-connected the events, but I still get:

"2Array"

#949271
echo $listChildEvents; // output is "Array"

This is the standard behavior in PHP. The toolset_get_related_posts return value is an array of WP post objects, and when you try to echo an Array directly in PHP you get "Array". If you want to inspect the array values from the front-end, you can do something this:

echo print_r($listChildEvents, true);
// or write to the log file:
error_log(print_r($listChildEvents, true));

If you want to loop over those post objects and do something with them, you can use a foreach loop like this:

foreach( $listChildEvents as $listChild ) {
  echo print_r($listChild, true);
}

More information about Array syntax and debugging in WordPress:
http://php.net/manual/en/language.types.array.php
https://codex.wordpress.org/Debugging_in_WordPress

#949294

Very helpful, thanks. I understand what to do with it now