Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Update post meta in another post after draft -> publish transition WordPress

  • SOLVED

Hi all,

I have two post types: one called people and another called responses. The responses post type holds survey resources, that are linked back via and ACF relationship field to the associated person who was selected when submitting the response form. This meta field in the responses post type is sourcepost. The return type of the field as set in ACF is the post ID.

What I'm looking to do is copy some meta values over from the response to the associate person record. For example, when responding the user can upload a headshot -- so I'd like to set that as the thumbnail. Additionally there is a field to set the "didrespond" status to "Yes".

(Survey responses need to be manually verified before going live, hence the need of separating the persons "profile" from their responses to begin with.)

Here's what I have, which seems like it would be right from what I've read about transition_post_status, but nothing appears to be copying over. I'm assuming it has something to do with the post object. Survey responses initially are saved as drafts. When someone comes and verifies the response and switches to published is when I'd like to copy the meta values over to the associated post.

I found this Gutenberg issue (https://github.com/WordPress/gutenberg/issues/12897) which is maybe related(?), but I have Gutenberg disabled for these two post types at least so I'm not totally sure.

Is there another way to accomplish this or am i just missing something below?

function responses_run_on_publish_only( $new_status, $old_status, $post ) {
if ( ( 'publish' === $new_status && 'publish' !== $old_status )
&& 'responses' === $post->post_type
) {
$sourcepostid = get_post_meta( $post->ID, 'sourcepost' ); // return the associated person post ID
$didrespond = get_post_meta( $post->ID, 'didrespond'); // get the response value
$attachment_thumb = get_post_thumbnail_id($post->ID); // get the featured image if uploaded
update_post_meta( $sourcepostid, 'didrespond', $didrespond); // update person with response value
set_post_thumbnail( $sourcepostid, $attachment_thumb); // set the persons thumbnail
}
}
add_action( 'transition_post_status', 'responses_run_on_publish_only', 10, 3 );

Answers (6)

2022-02-05

Arnav Joy answers:

Do you have staging site?

2022-02-06

Mohamed Ahmed answers:

Hi Andrew,

If you want to copy or update values from "responses" post type to "people" when the new response published you will use this code


/******************************
* Copy meta values from custom post type to another
* Mohamed Ahmed
********************************

function prowpsite_do_after_post_publish(){

/* Return the meta value of post */
$sourcepostid= get_post_meta(get_the_ID(),"sourcepost"); return the associated person post ID
$didrespond= get_post_meta(get_the_ID(),"didrespond"); get the response value
$thumbnail_id = (int) get_post_meta(get_the_ID(), '_thumbnail_id', true ); // get the featured image if uploaded

/* Copy the meta to the other custom post type */
if(isset($didrespond))
update_post_meta($sourcepostid,"didrespond",$didrespond); // update person with response value
if(isset($thumbnail_id))
set_post_thumbnail( $sourcepostid, $thumbnail_id ); // set the persons thumbnail

}

/* publish_responses = post type is responses you can change it to any custom post type */
add_action( 'publish_responses', 'prowpsite_do_after_post_publish', 10, 2 );


2022-02-06

Bob answers:

You can try.

post_updated
https://developer.wordpress.org/reference/hooks/post_updated/

or
save_post
https://developer.wordpress.org/reference/hooks/save_post/

or

save_post_{$post_type}
https://developer.wordpress.org/reference/hooks/save_post_post-post_type/

2022-02-07

Cesar Contreras answers:

Your code looks great, just add a validation for when there is no featured image, you could try debugging the received data, confirm the name of your post type and custom fields.
just to confirm that you take correct data
function responses_run_on_publish_only( $new_status, $old_status, $post ) {
//Do stuff only when posts are actually transitioned from one status to another.
// And the post type is my CPT
if ( ( 'publish' === $new_status && 'publish' !== $old_status ) && 'responses' === $post->post_type){
$sourcepostid = get_post_meta( $post->ID, 'sourcepost' ); // return the associated person post ID
$didrespond = get_post_meta( $post->ID, 'didrespond'); // get the response value
$attachment_thumb = get_post_thumbnail_id($post->ID); // get the featured image if uploaded

update_post_meta( $sourcepostid, 'didrespond', $didrespond); // update person with response value
if ($attachment_thumb)
set_post_thumbnail( $sourcepostid, $attachment_thumb); // set the persons thumbnail

} else {
return;
}
}
add_action( 'transition_post_status', 'responses_run_on_publish_only', 10, 3 );

2022-02-06

Ali mosbah answers:

Could you explain more

2022-02-07

Echeverri answers:

what exactly do you require