Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Add post meta as post is published
- Plugin URI: https://www.damiencarbery.com
- Description: Why isn't post meta with an underscore getting into the database?
- Author: Damien Carbery
- Version: 0.1
- */
- // When a post is published (status changes from draft to publish, add a post meta key.
- add_action( 'draft_to_publish', 'add_postmeta_on_publish' );
- function add_postmeta_on_publish( $post ) {
- error_log( 'eca_draft_to_publish: post_type: ' . $post->post_type );
- error_log( 'eca_draft_to_publish: post ID: ' . $post->ID );
- $meta_id = update_post_meta( $post->ID, 'dcwd_before', 'before' );
- error_log( 'eca_draft_to_publish: dcwd meta ID: ' . $meta_id );
- $meta_id = update_post_meta( $post->ID, '_em_waiting_list_check', 'yes' );
- error_log( 'eca_draft_to_publish: waiting_list meta ID: ' . $meta_id );
- $meta_id = update_post_meta( $post->ID, 'dcwd_after', 'after' );
- error_log( 'eca_draft_to_publish: dcwd meta ID: ' . $meta_id );
- }
- // Watch the hook that runs after postmeta is added and report the row ID ($mid) etc.
- add_action( "added_post_meta", 'dcwd_added_post_meta', 10, 4 );
- function dcwd_added_post_meta( $mid, $object_id, $meta_key, $_meta_value ) {
- error_log( 'dcwd_added_post_meta: mid: ' . $mid );
- error_log( 'dcwd_added_post_meta: object_id: ' . $object_id );
- error_log( 'dcwd_added_post_meta: meta_key: ' . $meta_key );
- error_log( 'dcwd_added_post_meta: _meta_value: ' . $_meta_value );
- }
- /* Here is the debug.log contents:
- [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: mid: 751
- [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: object_id: 203
- [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: meta_key: dcwd_before
- [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: _meta_value: before
- [08-Feb-2020 15:15:19 UTC] eca_draft_to_publish: dcwd meta ID: 751
- [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: mid: 752
- [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: object_id: 203
- [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: meta_key: _em_waiting_list_check
- [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: _meta_value: yes
- [08-Feb-2020 15:15:19 UTC] eca_draft_to_publish: add meta ID: 752
- [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 ***
- [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: mid: 753
- [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: object_id: 203
- [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: meta_key: dcwd_after
- [08-Feb-2020 15:15:19 UTC] eca_added_post_meta: _meta_value: after
- [08-Feb-2020 15:15:19 UTC] eca_draft_to_publish: dcwd meta ID: 753
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement