1

Below is my code for updating post meta.

function changePostExpireDatetime( $post ){
  $featurePlanID = get_post_meta($post->ID, 'post_price_plan_id', true );

  remove_action('pending_to_publish', 'changePostExpireDatetime', 10, 1);
  $plan_price = get_post_meta($featurePlanID, 'plan_price', true);
  update_post_meta($post->ID, 'post_plan_price', $plan_price );

  update_post_meta($post->ID, 'featured_post', "1" );
  add_action('pending_to_publish', 'changePostExpireDatetime', 10, 1);
}
add_action('pending_to_publish', 'changePostExpireDatetime', 10, 1);

Function is called but meta is was not updated. How can I update post meta in that hook.

2

I tend to make use of the transition_post_status hook as it gives you so much control over what you need to do. Be sure to check out the link for all available status options. What I specially like that the $post object is also passed to the hook, so you can target a specific post type as well for one.

You can try something like the following: (Just be sure to update the values to your own, I just used mine as example)

 add_action(  'transition_post_status',  function ( $new_status, $old_status, $post ) 
{
    // Check if we are transitioning from pending to publish
    if ( $old_status == 'pending'  &&  $new_status == 'publish' ) {
        // Check whether or not the meta_key exists already with our value
        if ( ! add_post_meta( $post->ID, 'post_views_count', 50, true ) ) { 
           update_post_meta ( $post->ID, 'post_views_count', 50 );
        }
    }
}, 10, 3 );
  • My pleasure, glad it worked out for you. Enjoy ;-) – Pieter Goosen Oct 2 '15 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.