Empty $object_id in rwmb_after_save_post

Support General Empty $object_id in rwmb_after_save_post

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #44102
    Catharina von HobeCatharina von Hobe
    Participant

    Hi,
    I want to add the stick_post() and unstick_post() functionallity to a custom post type and integrated a custom checkbox field for this.
    It's all working fine but the $object_id passed by the hook rwmb_after_save_post (as described in the documentation https://docs.metabox.io/actions/rwmb-after-save-post/) seems to be empty for me.

    I have made this relative easy checkbox field to stick and unstick the post:

    add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) {
    	$prefix = 'stick-post_';
    
    	$meta_boxes[] = [
    		'title'		 => __( 'Stick Post', 'cvh-design' ),
    		'id'		 => 'stick-posts',
    		'post_types' => [
    			'know-how'
    		],
    		'context'	 => 'side',
    		'style'		 => 'default',
    		'closed'	 => false,
    		'fields'	=> [
    			[
    				'type'		=> 'checkbox',
    				'id'		=> $prefix.'stick',
    				'desc'		=> _x('Make post sticky', 'backend', 'cvh_design'),
    				'columns'	=> 12,
    			],
    		]
    	];
    
    	return $meta_boxes;
    });

    And to check if the checkbox is ticked when the post is saved I created the following code.
    The isset($_POST['stick-post_stick']) works perfectly fine I checkt the value vai a wp_mail action.
    But the $object_id is empty when I try to log it into a wp_mail.

    What am I missing?

    add_action('rwmb_stick-posts_after_save_post', 'set_sticky_status');
    
    function set_sticky_status($object_id) {
    	if(isset($_POST['stick-post_stick'])) {
    		stick_post($object_id);
    	}
    
    	else {
    		unstick_post($object_id);
    	}
    }

    Thanks in advance.

    #44106
    PeterPeter
    Moderator

    Hello,

    You can try to use the Classic Editor and use the code below to output the variable $object_id to see if it works:

    function set_sticky_status($object_id) {
        echo $object_id;
        die('test12345');
    }
    #44123
    Catharina von HobeCatharina von Hobe
    Participant

    Hello Peter,
    your code does log me the $object_id, but I cannot use the classic Editor for this post type.
    And my Email logging I tried before with and isset($object_id) results to false.

    #44124
    PeterPeter
    Moderator

    Hello,

    How do you add the variable $object_id to the email? I use the sample code below to add the object ID to the wp_mail() function and use the plugin WP Mail Logging to log the email and the object ID is logged correctly.

    function set_sticky_status($object_id) {
        $to = '[email protected]';
        $subject = 'The subject';
        $body = 'Object ID: ' . $object_id;
        $headers = array('Content-Type: text/html; charset=UTF-8');
    
        wp_mail( $to, $subject, $body, $headers );
    }

    https://wordpress.org/plugins/wp-mail-logging/

    #44130
    Catharina von HobeCatharina von Hobe
    Participant

    Hello,
    I didnt include the headers but I logged the $_POST['stick-post_stick'] the same way.
    wp_mail('[email protected]', 'Sticky Post Test', "Post : " . $object_id);
    Also suddenly the variable isnt empty, maybe I had a typo somewhere.

    But the post still doesnt get stuck, any ideas for that?

    I tested the sticking functionallity by hardcoding a post id of my custom post type into the stick_post() function, so that shouldnt be the problem.
    And var_dump(get_option('sticky_posts')) displayed me this id in the array.

    #44139
    PeterPeter
    Moderator

    Hello,

    So the action hook rwmb_{$field_group_id}_after_save_post is working properly, just the issue with your custom code in this case.
    I will mark this ticket as Resolved here.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.