Skip Navigation

[Resolved] Using PHP to get the parent post ID when there are multiple parents

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 2 replies, has 1 voice.

Last updated by Gavin 4 months, 1 week ago.

Author
Posts
#2755712

Hello

I've tried very hard to get this to work but to no avail. I would appreciate it if one of you could give me a nudge in the right direction. It's a fairly simple bit of logic or code but I can't get it to work.

I have a CPT called Mailbox that is in a many-to-many relationship with another CPT called Conversation.

Mailboxes [*] <<>> Conversations [*]

The relationship slug is 'mailbox-conversation'.

Typically a Conversation has one Author and is connected to two parent Mailboxes each with an Author.

I have a content template for a Conversation and in that template I need a shortcode to check if the current logged in user ID has the same ID as the author of one of parent Mailbox posts. I'm happy for the shortcode to return true or false.

(The reason for wanting this shortcode is that Conversations are private and so I only want to display the conversation data if they are connected to that user's parent Mailbox, which I can do with a conditional in the template using the above shortcode. i.e. If the Conversation is connected to the current user's Mailbox then display the conversation)

I have tried lots of different PHP code using the toolset_get_related_posts API but couldn't get it to work. e.g.

$mailboxes = toolset_get_related_posts( $post_id, 'mailbox-conversation', array('query_by_role' => 'child', 'role_to_return' => 'parent'));

Many thanks for any tips!

Gavin

#2755845

I am getting closer:

$post_id = ( empty( $post->ID ) ) ? get_the_ID() : $post->ID;
$pmailboxes = toolset_get_related_posts( $post_id,'mailbox-conversation',array('query_by_role' => 'child','role_to_return' => 'parent'));

return implode( ', ', $pmailboxes );

This returns an array of IDs of the Parent mailboxes.

#2755901

I think I've done enough...

$post_id = ( empty( $post->ID ) ) ? get_the_ID() : $post->ID;

$pmailboxes = toolset_get_related_posts( $post_id,'mailbox-conversation',array('query_by_role' => 'child','role_to_return' => 'parent'));

$currentuser = get_current_user_id();

if ( is_array( $pmailboxes ) ) {
foreach ($pmailboxes as $key => $imailbox) {

$authorid = get_post_field( 'post_author', $imailbox );

if ($currentuser == $authorid) {
return $authorid;
}

}
}

#2755930

Will leave this here in case anyone else needs it!