Newer New solution:
function wpse153622_transition_solution( $new_status, $old_status, $post ) {
if ( $new_status != $old_status && 'publish' === $new_status && 'my-solution' === $post->post_type ) {
update_post_meta( $post_id, 'my_json', 'json' );
}
}
add_action( 'transition_post_status', 'wpse153622_transition_solution', 10, 3 );
This should trigger only when 1. the new post status isn’t the same as the old one 2. new post status is equal to publish and 3. post type is equal to your post type.
New Solution
Edit: After reading up on post status transitions, I think this should do the trick:
function wpse153622_save_solution( $post_id, $post ) {
update_post_meta( $post_id, 'my_json', 'json' );
}
add_action( 'publish_my-solution', 'wpse153622_save_solution', 10, 2 );
This uses publish_my-solution
, which should only trigger whenever a post of type my-solution
is published. See {status}_{post_type}
here.
Old solution
Using the save_post_{post_type}
action will the job for you, though it’ll be called whenever you update the post as well as publish:
function wpse153622_save_solution( $post_id, $post, $update ) {
update_post_meta( $post_id, 'my_json', 'json' );
}
add_action( 'save_post_my-solution', 'wpse153622_save_solution', 10, 3 );
I’ve used this before and it works, but like I said, it’ll be called whenever you make changes to the post. I don’t think that’ll be much of an issue, though.