0

I have created a function that triggers and connect parent post with Child post based on a Custom Meta Field value. If Meta field value of an existing Parent post matches with the newly created child post then function automatically connect the parent post with child post. and this function works perfectly If I am creating the child post using WP Admin area or using a Frontend Post Submission form. But If I am creating the Child Post usign a Webhook Incoming data then function doesn't trigger. and it doesn't connect the parent post based on the Custom Meta field "wpcf-patient-id".

I am using WP Webhooks Pro to receive data from a external source that includes the post title, content, wpcf-patient-id, and few other details. Each time Webhook receives a payload data, it creates a new post using the received payload data. But the function doesn't trigger at all.

So if anyone can help me make it work, Please suggest the possible solution.

add_action( 'save_post', 'assign_parent_patient_func', 100,3 );
function assign_parent_patient_func( $post_id, $post, $update ) {
    
    // Only set for post_type = post!
    if ( !in_array($post->post_type, array('qev-attachment', 'note')) ) {
        return;
    }
      
    $patient_id = get_post_meta($post_id, 'wpcf-patient-id', true); 
    if($patient_id){
        $client_ids = get_posts( array(
            'post_type' => 'client',
            'meta_key' => 'wpcf-patient-id', 
            'meta_value'=> $patient_id,
            'fields' => 'ids'
        ));
        if(isset($client_ids[0])){
            $client_id = $client_ids[0];
            if($post->post_type == 'qev-attachment'){
                toolset_connect_posts('client-attachment', $client_id, $post_id);
            }
            if($post->post_type == 'note'){
                toolset_connect_posts('client-note', $client_id, $post_id);
            }
        }
    }
}

1
  • Unfortunately, you will need to pose this question in the official support channels for that webhook plugin.
    – bosco
    Commented Aug 2, 2021 at 22:37

1 Answer 1

0

From what I see is the issue within your code. Your issue is that you try to fetch the post type in your nested if-statements from the current post, even though at the first if statement, you validate against it and break the function.

What you want to do instead is to validate the post type against the $client_id variable.

$client_parent = get_post( $client_id );
$post_type = '';
if( ! empty( $client_parent ) && isset( $client_parent->post_type ) ){
    $post_type = $client_parent->post_type;
    if( $post_type === 'note' ){
        //run your logic
    }
}

In case it's still not working, you might want to take a look at the documentation of the update_post webhook action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.