Skip Navigation

[Resolved] Getting a post relationship in PHP

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

Problem: I have some questions about the toolset_get_related_posts API.
- I'm querying for child posts. Why do I query_by_role "parent"?
- If the result of the API is an array with one post, how do I access that one post without a foreach loop?
- Can you update the documentation to be more helpful?

Solution:
- You should use query_by_role "parent" when the known post is the parent post and you want to retrieve child posts.
- You can access an array item by zero-based index using the syntax here:

$item = $array[0];

- I've asked for some documentation updates here and our team will make the necessary improvements.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

This support ticket is created 5 years, 7 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. 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 2 replies, has 2 voices.

Last updated by KenG8581 5 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#1225106

I have tried to find my way using the documentation, but I must be honest to say that this area is not very well documented. There is even a chance to get confused due to the legacy examples etc.

Anyway - here is what I am trying to do.

I have three Post types (in this case).
Clients (can have multiple Risk questionnaires)
Risk questionnaires (can have max 1 Client - can have max 1 Risk control)
Risk controls (can have max 1 Risk questionnaire)
So - an instance of a one-many and a one-one relationship.

When a user creates a new Risk controls post, the relationship with the Risk questionnaire, marked current, must be set in the backend. On 'cred_save_data' I would like to find the current user's Clients current Risk questionnaire (in the below example the first post with current =1), and then connect the newly created Risk controls post with the found Risk questionnaire.

I got it to work, but only after struggling with it for a while.
Here are my comments and questions:

1. Couldnt get it to work until I modified the 'query_by_role' to 'parent'. Intuitively I used 'child' as the query is actually performed on the child posts of the Client->Risk questionnaires relationship. How come I should use 'parent'?
2. How do I use the single post_id returned in the array $needs_quests and avoid the foreach loop?
3. Can you please improve the important documentation around relationships, or at least guide me to a place in the documentation where all the parameters are explained, including what is mandatory (eg. I dont know why I include the 'offset' parameter!) - and some examples please.

Thanks in advance

// Connect a new Risk controls posst with the current Needs questionnaire
add_action('cred_save_data_7805','tmbd_save_data',10,2);
function tmbd_save_data($risk_control_id, $form_data) {
  // Find the ClientID of the current user
  $user_client_id = types_render_usermeta( "userclientid", array( "user_current" => true, "output" => "raw" ));
  
  // Find the current (active) Client Needs risk questionnaire
  $needs_quests = toolset_get_related_posts (
    $user_client_id,
    'client-risk-questionnaire',
    array(
      'query_by_role' => 'parent',
      'role_to_return' => 'child',
      'return' => 'post_id',
      'limit' => 1,
      'offset' => 0,
      
      'args' => array(
        'meta_key'   => 'wpcf-current',
        'meta_value' => 1), // for adding meta queries etc.
        'post_status' => array ('publish')
      )
    );
    
    // Loop through array of questionnaires - but there should only be one actually
    foreach ($needs_quests as $needs_quest) {
      toolset_connect_posts( 'risk-questionnaire-risk-control', $needs_quest, $risk_control_id );
    }
  }
#1225311

1. Couldnt get it to work until I modified the 'query_by_role' to 'parent'
If you want to get results from the child post type but you know the parent post ID, then the query by role field should be parent. Parent is the "known" value in this case. If you were querying for the parent based on a known child ID, you would query by child.

2. How do I use the single post_id returned in the array $needs_quests and avoid the foreach loop?
In PHP you can access an array item by numeric index:

$needs_quests[0];

https://www.geeksforgeeks.org/php-arrays/
Your code should also handle unexpected cases, like if somehow no results are returned. That's one reason the foreach loop is practical - if no results are found the connect posts function is not called with invalid data.

3. Can you please improve the important documentation around relationships, or at least guide me to a place in the documentation where all the parameters are explained, including what is mandatory (eg. I dont know why I include the 'offset' parameter!) - and some examples please.
I've submitted a ticket to our documentation team requesting some updates here. I agree the examples list is not comprehensive enough to explain the nuances of both syntaxes, and it's not clear which arguments are required or optional.

#1225641

My issue is resolved now. Thank you!