Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. add_action('publish_post', 'add_custom_field_automatically');
  2. function add_custom_field_automatically($post_ID) {
  3. global $wpdb;
  4.  
  5. if(!wp_is_post_revision($post_ID)) {
  6. update_post_meta($post_ID, 'votes_count', '0');
  7.  
  8. }
  9. }
  10.  
  11. add_action( 'transition_post_status', 'wpse120996_post_status_publish', 10, 3 );
  12. function wpse120996_post_status_publish( $new_status, $old_status, $post_ID ) {
  13. if ( $new_status == 'publish' && $old_status == 'pending' ) {
  14. global $wpdb;
  15.  
  16. if(!wp_is_post_revision($post_ID)) {
  17. update_post_meta($post_ID, 'votes_count', '0');
  18. }
  19. }}
  20.  
  21. add_action('publish_post', 'wpse120996_add_custom_field_automatically');
  22. function wpse120996_add_custom_field_automatically($post_id) {
  23. global $wpdb;
  24. $votes_count = get_post_meta($post_id, 'votes_count', true);
  25. if( empty( $votes_count ) && ! wp_is_post_revision( $post_id ) ) {
  26. update_post_meta($post_id, 'votes_count', '0');
  27. }
  28. }
  29.  
  30. //it's specific because you specify the hook like this {$old_status}_to_{$new_status}
  31. add_action( 'auto-draft_to_publish', 'wpse120996_specific_post_status_transition' );
  32. function wpse120996_specific_post_status_transition() {
  33. //your code
  34. }
  35.  
  36. //it's generic because you specify post statuses inside the function not via the hook
  37. add_action( 'transition_post_status', 'wpse120996_generic_post_status_transition', 10, 3 );
  38. function wpse120996_generic_post_status_transition( $new_status, $old_status, $post ) {
  39. if ( $new_status == 'publish' && $old_status == 'auto-draft' ) {
  40. //your code
  41. }
  42. }
  43.  
  44. add_action('save_post', 'wpse120996_on_creation_not_update');
  45. function wpse120996_on_creation_not_update($post_id) {
  46. //get_post( $post_id ) == null checks if the post is not yet in the database
  47. if( get_post( $post_id ) == null ) {
  48. //your code
  49. }
  50. }
  51.  
  52. add_action('publish_post', 'add_custom_field_automatically');
  53. function add_custom_field_automatically($post_ID) {
  54.  
  55. global $wpdb;
  56.  
  57. $meta_count = get_post_meta($post_ID, "votes_count", true);
  58. if($meta_count == '') {
  59. if(!wp_is_post_revision($post_ID)) {
  60. update_post_meta($post_ID, 'votes_count', '0');
  61.  
  62. }
  63. }
  64.  
  65. }
  66.  
  67. add_action( 'publish_post' , 'my_func' , 10 , 2 );
  68. function my_func( $ID , $post )
  69. {
  70. if ( $post->post_date != $post->post_modified )
  71. {
  72. //THIS IS AN UPDATE
  73. }
  74. else
  75. {
  76. //POST JUST GOT PUBLISHED
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement