[wp-trac] [WordPress Trac] #41751: save_post_{custom_post_type} not behaving exactly as save_post

WordPress Trac noreply at wordpress.org
Wed Aug 30 17:57:34 UTC 2017


#41751: save_post_{custom_post_type} not behaving exactly as save_post
-------------------------------+------------------------------
 Reporter:  dipakbbsr          |       Owner:
     Type:  defect (bug)       |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  Posts, Post Types  |     Version:  4.8.1
 Severity:  normal             |  Resolution:
 Keywords:                     |     Focuses:  administration
-------------------------------+------------------------------
Changes (by dipakbbsr):

 * keywords:  reporter-feedback =>


Comment:

 Hi,

 I want to clarify as much as I can.

 To answer your 1st question, "Yes, the post type is created by hand
 written code using ''register_post_type'' and i am attaching the
 properties below,

 {{{#!php
 <?php
 $args = array(
         'label'                 => __( 'Redeem', 'wp-coupon' ),
         'description'           => __( 'Post type is used to gather user
 data regarding a request to redeem reward points', 'wp-coupon' ),
         'labels'                => $labels,
         'supports'              => array( 'title', 'author', ),
         'hierarchical'          => false,
         'public'                => true,
         'show_ui'               => true,
         'show_in_menu'          => true,
         'menu_position'         => 5,
         'show_in_admin_bar'     => false,
         'show_in_nav_menus'     => true,
         'can_export'            => false,
         'has_archive'           => false,
         'exclude_from_search'   => true,
         'publicly_queryable'    => true,
         'capability_type'       => 'page',
 );
 register_post_type( 'redeem', $args );
 }}}

 And the meta fields and meta boxes are handled by ACF Pro.

 Meta fields for "Redeem" post types are,
 1. Shop Name
 2. Amount
 3. Redeem Status ( Default Pending & Will be updated to Accepted or
 Rejected by admin )
 4. Award Point ( When Accepted is selected by admin, he has to set a point
 that will be added to user meta when published )

 Front end submission form is created by ''acf_form'', Which only shows
 meta filed 1 & 2. '''Note''' : meta field 3 & 4 will only be updated when
 admin publish the post.

 So now post is created with 2 meta fields in database, as acf_form is used
 it is not adding other meta fields which are not in form. Admin can see
 the post with status "Draft"and goes to edit that. Checks the Redeem
 Status to "Accepted" (Number 3 meta field), then gives a value for number
 4 meta field (Award Point, which will be updated to user meta ) and clicks
 on submit.

 Here Answering your other questions,
 1. Yes, new values are being saved to database.
 2. No revision is being created
 3. Yes, original ( that is one and only ) post's status changing to
 Publish.

 Everything else is working fine except the code written inside action
 '''save_post_redeem'''''''

 {{{#!php
 <?php
 function save_update_redeem_requests( $post_id, $post, $update ) {

     $post_type = get_post_type($post_id);
     if ( "redeem" != $post_type )
         return;

     if( $post->post_status && $post->post_status == 'publish' ) {

         $points_to_add = get_post_meta( $post_id, 'reward_point', true );
         update_user_meta( $post->post_author, 'mycred_default',
 $points_to_add );
     }
 }
 add_action( 'save_post_redeem', 'save_update_redeem_requests', 12, 3 );
 }}}

 There are other condition like if status == accepted and similar things, i
 have omitted them to explain with less code, also i check everything with
 less code.

 Point is above code works perfectly with save_post. But with
 save_post_redeem its unable to find value of 3rd and 4th meta field, that
 were not created while post creation, they are being updated now when
 admin wish to make the post publish. DB is updating but i cant get the
 meta fields for manipulation.

 {{{#!php
 <?php
 add_action( 'save_post', 'save_update_redeem_requests', 12, 3 );
 }}}

 Above code works superbly.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/41751#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list