Skip Navigation

[Resolved] Determine if 2 Posts are related

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

Problem:

The user is seeking the most efficient way to determine if two posts are related using Toolset. They have developed a function but find it inefficient as it connects and disconnects the posts. They are also exploring alternatives.

Solution:

For simple cases, HTML conditionals or Custom Code can be used. Check the following links for examples:

https://toolset.com/forums/topic/how-to-test-for-related-posts-to-post-of-current-view/#post-1522445

https://toolset.com/forums/topic/conditional-check-if-there-are-related-posts/

But for the dynamic detection, the customer suggested their solution:

https://toolset.com/forums/topic/determine-if-2-posts-are-related/#post-2678509

Documentation:

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

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.

This topic contains 4 replies, has 2 voices.

Last updated by Christopher Amirian 10 months, 1 week ago.

Assisted by: Christopher Amirian.

Author
Posts
#2678211

Tell us what you are trying to do? I am trying to find the most efficient way to determine if 2 posts are related.

Is there any documentation that you are following?
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

Using the above, I have developed:

function are_related( $residentID, $p2pID ): bool {
if (toolset_connect_posts( 'tsrresident-p2p', $residentID, $p2pID )['success'] ){
toolset_disconnect_posts( 'tsrresident-p2p', $residentID, $p2pID );
return false;
}else{
return true;
}
}

This seems inefficient as it will connect the posts and then disconnect them just to find out if they are related.

I could also determine if there is an Intermediary Post, but that seems even less efficient.

Is there a similar example that we can see?

What is the link to your site?

#2678276

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

In simple cases you can use HTML conditional:
https://toolset.com/forums/topic/how-to-test-for-related-posts-to-post-of-current-view/#post-1522445

Or you can use the Custom Code here if it's more a complex scenario
https://toolset.com/forums/topic/conditional-check-if-there-are-related-posts/

Thanks.

#2678285

Thanks Christopher.

BTW, what happened to all the usage examples in the api documentation. When I attempt to view I get
[types field='toolset-api-example' output='raw'][/types]

#2678298

Christopher,

Your provided solution only indicates how many child posts are related to a given parent. My question was "How can I determine if a specific parent was related to a specific child. I came up with another solution as indicated below. It returns not only if the specific parent and child are related but also the id of the intermediary post. Do you see a more efficient way to accomplish?

function get_intermediary_postID( $residentID, $p2pID ): array { //associative array with related and ID Keys
$out_parent = toolset_get_related_posts(
$residentID,
'tsrresident-p2p',
'parent',
100,
0,
array(),
'post_id',
'intermediary'
);
$out_child = toolset_get_related_posts(
$p2pID,
'tsrresident-p2p',
'child',
100,
0,
array(),
'post_id',
'intermediary'
);
$out = array_intersect( $out_child, $out_parent );
if ( empty( $out ) ) {
$r = array( "related" => false, "id" => 'NA' );
} else {
$r = array( "related" => true, "id" => reset( $out ) ); //returns first value in the associative array
}

return $r;
}

#2678509

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

Your solution appears well-structured for determining if a specific parent is related to a specific child and retrieving the ID of the intermediary post. However, here are a few suggestions for improvement:

Error Handling:

Validate input parameters ($residentID and $p2pID) to ensure they are numeric before proceeding with the queries. This helps prevent issues with invalid inputs.

Query Limit:

Consider whether the fixed limit of 100 for returned results is sufficient for all scenarios. Adjust it based on your specific needs or implement pagination if there could be more than 100 related posts.

Array Functions:

The use of array_intersect and reset is appropriate, assuming there is only one intermediary post related to the given parent-child relationship. If multiple intermediary posts are possible, adapt the logic accordingly.

Something like this:

function get_intermediary_postID($residentID, $p2pID): array {
    // Validate input parameters
    if (!is_numeric($residentID) || !is_numeric($p2pID)) {
        return array("related" => false, "id" => 'Invalid input');
    }

    // Query related posts for parent and child
    $out_parent = toolset_get_related_posts($residentID, 'tsrresident-p2p', 'parent', 100, 0, array(), 'post_id', 'intermediary');
    $out_child  = toolset_get_related_posts($p2pID, 'tsrresident-p2p', 'child', 100, 0, array(), 'post_id', 'intermediary');

    // Check for the intersection of related posts
    $out = array_intersect($out_child, $out_parent);

    // Determine the result
    if (empty($out)) {
        return array("related" => false, "id" => 'NA');
    } else {
        // If there are multiple intermediary posts, handle it appropriately
        return array("related" => true, "id" => reset($out));
    }
}

Please consider that I did not test the above code so it is just a general suggestion.

Thanks.