Skip Navigation

[Resolved] unable to retrieve relationship field in toolset_association_created

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

Problem:

When using the toolset_association_created hook to retrieve a relationship field value from the intermediary post, the custom field returned an empty value, even though the value was confirmed to exist in the database. The field was stored on the intermediary post, and get_post_meta() was being called inside the hook, but it returned blank.

Solution:

The issue was caused by timing. At the moment toolset_association_created fires, the intermediary post meta may not yet be fully saved and available for retrieval.

To resolve this, the meta retrieval was delayed until after Toolset completed saving the relationship. This was achieved by scheduling a secondary action using wp_schedule_single_event() and retrieving the field value in a later hook.

Confirmed the intermediary post exists and has the expected post type and status.

Verified the correct meta key (wpcf-donor-scholar-relationship-status) is stored on the intermediary post.

Ensured the field is indeed saved on the intermediary (relationship fields are not stored on parent/child posts).

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

Last updated by raffyC 1 month ago.

Assisted by: Christopher Amirian.

Author
Posts
#2847692

Hi,

I need to retrieve a relationship field when connecting two posts but it's returning blank. I have this simple function where in based on the intermediary_id, I should fetch the field ($donor_rel) but it's returning blank, I verified in the database that there is a value in the post meta field. Did I miss anything?

add_action( 'toolset_association_created', 'add_donor_scholar_association', 10, 5 );
function add_donor_scholar_association( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ){
error_log("association ". $association_slug." parent_id:".$parent_id." child_id:".$child_id." intermediary_id:".$intermediary_id." association_id:".$association_id );

if ( $association_slug == 'donor-scholar' ) {
$donor_rel = get_post_meta( $intermediary_id, 'wpcf-donor-scholar-relationship-status', true );
error_log("donor_rel:".$donor_rel);

...

#2847732

Christopher Amirian
Supporter

Languages: English (English )

Hi,

Welcome to Toolset support. I suggest that you test the steps below:

- Confirm the intermediary really exists and is the one you expect

Log the intermediary post type/status and ensure it’s not a draft/autosave:

$intermediary = get_post( $intermediary_id );
error_log( print_r( array(
    'ID' => $intermediary_id,
    'post_type' => $intermediary ? $intermediary->post_type : 'no-post',
    'post_status' => $intermediary ? $intermediary->post_status : 'no-post',
), true ) );

- If meta is empty in this hook, run your read after Toolset completes saving

add_action( 'toolset_association_created', function( $slug, $parent_id, $child_id, $intermediary_id, $association_id ) {

    if ( $slug !== 'donor-scholar' ) {
        return;
    }

    wp_schedule_single_event( time(), 'chr_read_donor_scholar_intermediary', array( $intermediary_id ) );

}, 10, 5 );

add_action( 'chr_read_donor_scholar_intermediary', function( $intermediary_id ) {

    $donor_rel = get_post_meta( $intermediary_id, 'wpcf-donor-scholar-relationship-status', true );
    error_log( 'donor_rel (after save): ' . $donor_rel );

} );

Also verify the meta ke. Make sure wpcf-donor-scholar-relationship-status is the actual field slug stored on the intermediary post (relationship fields are stored on the intermediary, not on the parent/child posts).

For more information about Relationships API:

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

Thanks.

#2847832

Your suggestion worked. Issue resolved. Thank you!