Tell us what you are trying to do?
I have two custom post types (Player Profiles as parent, Seasons as child) which are in a m2m relationship (player-profile-season). In the WP Admin and programmatically both, I have connected a Player Profile and a Season - this connection shows on the WP Admin side and returns true when done via toolset_connect_posts(). I have checked the database and confirmed that both the Player Profile and Season have entries in the tables toolset_connected_elements and toolset_associations, and that the entries point to the correct IDs for the content items and the relationship between them. I have also verified that toolset_get_related_posts() works if I try and get the connected posts for a different o2m relationship. However, when I try to use toolset_get_related_posts() to get all the Seasons related to a Player Profile (or when I use it with any of my other m2m relationships), I am returned an empty array when I expect to see the relationship I created earlier.
Is there any documentation that you are following?
The documentation found here: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
Is there a similar example that we can see?
Here is the code I am currently using, where $season_ids is populated as an empty array when it should be populated with the record I see in the database and on the WP Admin side:
$season_ids = toolset_get_related_posts(
$player_profile->ID,
'player-profile-season',
[
'query_by_role' => 'parent',
'need_found_rows' => true,
]
);
Notes:
- I have verified that $player_profile->ID returns the correct post ID
- I have verified that 'player-profile-season' is the correct relationship slug
- I have verified that the Player Profile post type is marked as the parent in the relationship
What is the link to your site?
I can provide this if needed - it is a development site and password protected, but I would like to know if I am missing something critical first.
The issue is that by default, toolset_get_related_posts() does not recognize posts in the 'private' status - you must specifically specify 'post_status' => 'private' in the $args parameter. See below code for the working example.
Note: the relationship_slug was something changed during debugging, it was not the cause of the issue.
Note: you also seem to need to specify 'role_to_return' as 'all', else you don't get any results back either.
$relationship_slug = 'playerprofile-season';
$args = [
'query_by_role' => 'parent',
'role_to_return' => 'all',
'args' => ['post_status' => 'private']
];
$season_ids = toolset_get_related_posts( $player_profile->ID, $relationship_slug, $args );
Hope this is helpful to someone else in the future.