Advertisement
daymobrew

Add postmeta on post publish

Feb 8th, 2020
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.59 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Add post meta as post is published
  4. Plugin URI: https://www.damiencarbery.com
  5. Description: Why isn't post meta with an underscore getting into the database?
  6. Author: Damien Carbery
  7. Version: 0.1
  8. */
  9.  
  10.  
  11. // When a post is published (status changes from draft to publish, add a post meta key.
  12. add_action( 'draft_to_publish', 'add_postmeta_on_publish' );
  13. function add_postmeta_on_publish( $post ) {
  14.         error_log( 'eca_draft_to_publish: post_type: ' . $post->post_type );
  15.         error_log( 'eca_draft_to_publish: post ID: ' . $post->ID );
  16.  
  17.         $meta_id = update_post_meta( $post->ID, 'dcwd_before', 'before' );
  18.         error_log( 'eca_draft_to_publish: dcwd meta ID: ' . $meta_id );
  19.         $meta_id = update_post_meta( $post->ID, '_em_waiting_list_check', 'yes' );
  20.         error_log( 'eca_draft_to_publish: waiting_list meta ID: ' . $meta_id );
  21.         $meta_id = update_post_meta( $post->ID, 'dcwd_after', 'after' );
  22.         error_log( 'eca_draft_to_publish: dcwd meta ID: ' . $meta_id );
  23. }
  24.  
  25.  
  26. // Watch the hook that runs after postmeta is added and report the row ID ($mid) etc.
  27. add_action( "added_post_meta", 'dcwd_added_post_meta', 10, 4 );
  28. function dcwd_added_post_meta( $mid, $object_id, $meta_key, $_meta_value ) {
  29.     error_log( 'dcwd_added_post_meta: mid: ' . $mid );
  30.     error_log( 'dcwd_added_post_meta: object_id: ' . $object_id );
  31.     error_log( 'dcwd_added_post_meta: meta_key: ' . $meta_key );
  32.     error_log( 'dcwd_added_post_meta: _meta_value: ' . $_meta_value );
  33. }
  34.  
  35. /* Here is the debug.log contents:
  36. [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: mid: 751
  37. [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: object_id: 203
  38. [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: meta_key: dcwd_before
  39. [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: _meta_value: before
  40. [08-Feb-2020 15:15:19 UTC] eca_draft_to_publish: dcwd meta ID: 751
  41. [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: mid: 752
  42. [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: object_id: 203
  43. [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: meta_key: _em_waiting_list_check
  44. [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: _meta_value: yes
  45. [08-Feb-2020 15:15:19 UTC] eca_draft_to_publish: add meta ID: 752
  46. [08-Feb-2020 15:15:19 UTC] eca_draft_to_publish: waiting_list meta ID:   *** Note that this is blank but prev lines show it worked ***
  47. [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: mid: 753
  48. [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: object_id: 203
  49. [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: meta_key: dcwd_after
  50. [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: _meta_value: after
  51. [08-Feb-2020 15:15:19 UTC] eca_draft_to_publish: dcwd meta ID: 753
  52. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement