Skip Navigation

[Resolved] Get Post Reference field data directly in PHP

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

Problem:

I am trying to retrieve the post ID of a related post connected through a specific Toolset Post Reference Field in PHP, but using get_post_meta() returns an empty string, and I need to avoid fetching all related posts of the same type.

Solution:

Use toolset_get_related_post( $post_id, 'relationship-slug' ) to retrieve the post ID tied specifically to the Post Reference Field; it returns the related post ID or 0 if none is found.

Example:

$related_post_id = toolset_get_related_post( 123, 'advisor' );
if ( $related_post_id ) {
    echo 'Related post ID: ' . $related_post_id;
} else {
    echo 'No related post found.';
}

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/views-filters/

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

Last updated by Doug 3 weeks ago.

Assisted by: Christopher Amirian.

Author
Posts
#2807598

I'm trying to pull some Toolset data into PHP so that I can use it within functions. I've successfully been able to pull info from custom fields using toolset_get_related_posts() which I've been using in some other functions, but I have a situation in which I'm not sure how to pull the correct data.

I need to grab the post-id of a related post that was connected using a Post Reference Field. The problem is that there are other related posts with that same post type that are also connected, but I need to grab only the related post referenced with that particular Post Reference Field, so I can't just grab all related posts of that post type using a normal query.

I tried grabbing the Post Reference Field directly using get_post_meta() as I would for normal field types, hoping that maybe it would return a post object or post id, but I just get an empty string. So I imagine I'm implementing it incorrectly, or have to use a query with some type of filter or something?

How can I access the data within a Post Reference field in PHP?

#2807741

Christopher Amirian
Supporter

Languages: English (English )

Hi,

Welcome to Toolset support.

To retrieve the post ID of a related post connected via a specific Post Reference Field in Toolset, you can use the toolset_get_related_post() function. This function is designed to fetch the related post ID for a given post and relationship field.

$related_post_id = toolset_get_related_post( $post_id, 'relationship-slug' );

Parameters:

$post_id (int): The ID of the post from which you want to get the related post.

'relationship-slug' (string): The slug of the relationship (i.e., the Post Reference Field) you want to query.

Return Value:

Returns the ID of the related post if found.

Returns 0 if no related post is found.

Example:

$related_post_id = toolset_get_related_post( 123, 'advisor' );
if ( $related_post_id ) {
    echo 'Related post ID: ' . $related_post_id;
} else {
    echo 'No related post found.';
}

For more information:

https://toolset.com/documentation/programmer-reference/views-filters/

Thanks.

#2807985

Thanks so much! Didn't realize it was so simple haha