Skip Navigation

[Waiting for user feedback] unable to retrieve relationship field in toolset_association_created

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 Christopher Amirian 23 minutes 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.