Skip Navigation

[Resolved] Save data to child-post field (update post meta) on creating a post relationship

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 5 replies, has 3 voices.

Last updated by Waqar 10 months ago.

Assisted by: Waqar.

Author
Posts
#2677544

I’m trying to handle a task equivalent, I think, to what’s addressed in the discussion here: https://toolset.com/forums/topic/when-using-the-hook-toolset_association_created-im-not-able-to-update-post-meta/page/2/#post-1763537

It may be that I’m misunderstanding something about the wpcf_fields_value_save filter. (Not sure where to go for documentation.)

Here’s my copy (with error logging included) of the two-step filter-action way of going about this that solved the issue in the previous thread:

function ic_update_task_assigned_worker_id_ref( $relation_slug, $parent_id, $child_id, $intermed_id, $relation_id ) {
  if ( $relation_slug === 'workers-and-task' ) {
    $default_value = $parent_id;
    add_filter( 'wpcf_fields_value_save', static function( $value, $field_type, $field_slug ) use( $default_value ) {
      if ( 'task-assigned-worker-postid' === $field_slug ) {
        $value = $default_value;
      }
      return $value;
    }, 10, 3 );
  }
  error_log( "relationship:" . $relation_slug );
  error_log( "parent_id:" . $parent_id );
  error_log( "child_id:" . $child_id );
  error_log( "intermediate_id:" . $intermed_id );
  error_log( "association_id:" . $relation_id );
}
add_action( 'toolset_association_created', 'ic_update_task_assigned_worker_id_ref', 10, 5 );

The error log result appears to check out. Parent and child post IDs given by the function are correct. For whatever reason, though, no value is being saved to the field in question.

What I want to achieve, in the end, seems relatively straightforward. When the post relationship is effected, I want a record of the parent-post ID held in a field I’ve provided for the purpose in the child post. (This is to facilitate some multi-filter front-end querying I’m doing with the post types elsewhere.)

Thanks in advance for your assistance!

#2677579

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

Your code looks rather more complicated than it needs to be.

I just tried something very similar on my local test site, where I have a project-task one-to-many relationship, and with the following code I stored the parent ID on the child post with the following code:

add_action( 'toolset_association_created', 'ts_handle_new_association', 10, 3 );
function ts_handle_new_association( $relationship_slug, $parent_id, $child_id ){
	
	if ( $relationship_slug == 'project-task' ) {

		update_post_meta( $child_id, 'wpcf-parent-id', $parent_id );
	}
	
}

Do you want to review that and update your own code accordingly?

#2677590

Thank you for the quick reply, Nigel. Your recommendation here returns me to my starting point. When the hook didn’t take, with first attempts, in the straightforward way it seemed it should work, I went looking for further info and found the 2020 ticket with your two-step maneuver. I have explored and tried a number of things since that initial test, though, and it probably is a good step at this point to do another round with a simple implementation, on chance that previous attempts were missing something. Will do that and follow up later today.

#2677667

Thanks for the update and glad Nigel's recommendation helped.

You're welcome to try it out and let us know if you have a follow-up question.

#2678588

My apologies. Circumstances with the project have prevented keeping this problem immediately to fore. I’ve handled it with a temporary workaround for the moment, but would like to keep the ticket open until I can return to it. It looks like I’m nearly clear to do that — within next day, I think.

#2678608

Thanks for the heads up and please take your time.

The ticket will stay open for a couple of weeks.

#2699854

I came back to this issue at some point after being overtaken with a number of others in course of work on the project, but without remembering the ticket here. The working version I ended up with, I’ll just note, is as simple as Nigel indicates, except that I see that I’ve included all five parameters in the parens. Not necessary, I recognize, and I no longer recall how this came into my rewrite. The other curious thing I notice, though, is that I’ve got this as a filter instead of an action, which seems contrary to the documentation. I don’t know now how I arrived at this. It might have been accidental. But the function seems to work either way.

function ic_task_worker_assignment_postmeta( $assoc_slug, $parent_id, $child_id, $intermed_id, $assoc_id ) {
    if ( $assoc_slug == 'workers-and-task' ) {
        update_post_meta( $child_id, 'wpcf-task-assigned-worker-postid', $parent_id );
    }
}
add_filter( 'toolset_association_created', 'ic_task_worker_assignment_postmeta', 10, 5 );