php - Run when a post-type is saved run a function in wordpress -
i trying add_action in wordpress such when post of type 'fep_message' saved check '_fep_delete_by_' keys associated post_parent id , delete them wp_post_meta table. code built not working:
add_action('publish_post', 'undelete_thread'); function undelete_thread($post_id, $post) { global $wpdb; if ($post->post_type = 'fep_message'){ $participants = fep_get_participants( $post->post_parent ); foreach( $participants $participant ) { $query ="select meta_id wp_postmeta post_id = %s , `meta_key` = '_fep_delete_by_%s'"; $queryp = $wpdb->prepare($query, array($post->post_parent, $participant)); if (!empty($queryp)) { delete_post_meta($queryp,'_fep_delete_by_' . $participant); } } } }
what proper hook done?
use save_post hook in wordpress. can find more info here
https://codex.wordpress.org/plugin_api/action_reference/save_post
the code should changed this:
add_action('save_post', 'undelete_thread'); function undelete_thread($post_id) { global $wpdb; global $post; if ($post->post_type = 'fep_message'){ $participants = fep_get_participants( $post->post_parent ); foreach( $participants $participant ) { $query ="select meta_id wp_postmeta post_id = %s , `meta_key` = '_fep_delete_by_%s'"; $queryp = $wpdb->prepare($query, array($post->post_parent, $participant)); if (!empty($queryp)) { delete_post_meta($queryp,'_fep_delete_by_' . $participant); } } } }
Comments
Post a Comment