Empty $object_id in rwmb_after_save_post
- This topic has 5 replies, 2 voices, and was last updated 1 year, 4 months ago by
Peter.
-
AuthorPosts
-
December 15, 2023 at 7:30 PM #44102
Catharina von Hobe
ParticipantHi,
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.
December 16, 2023 at 11:43 AM #44106Peter
ModeratorHello,
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'); }
December 18, 2023 at 5:39 PM #44123Catharina von Hobe
ParticipantHello 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.December 18, 2023 at 7:26 PM #44124Peter
ModeratorHello,
How do you add the variable
$object_id
to the email? I use the sample code below to add the object ID to thewp_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 ); }
December 18, 2023 at 9:55 PM #44130Catharina von Hobe
ParticipantHello,
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.December 19, 2023 at 7:02 PM #44139Peter
ModeratorHello,
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. -
AuthorPosts
- You must be logged in to reply to this topic.