Meta Box
Support › MB Custom Table › mbct_after_add $object_id empty
Hi guys,
Quick one - we're trying to use the mbct_after_update hook on a Custom Model but the retrieved $object_id variable is always empty.
https://docs.metabox.io/extensions/mb-custom-table/#hooks
Is this a bug that you can create at your end? Is so any chance you could push through a quick fix?
Cheers,
Will
Hi Will,
What is the code that you are using to use the $object_id? I've tested this action with a callback function and it displays the object ID as well.
add_action( 'mbct_after_update', function( $object_id, $table, $row ) { echo "Object ID is: " . $object_id . '<br>'; die( 'Stop here!' ); //for debug }, 10, 3);
And make sure that you have the latest version of MB Custom Table 2.1.1.
Apologies, It was the mbct_after_add not mbct_after_update hook that had the issue. I referenced the wrong hook in the body of the original post
Would you mind checking again for me?
Hi,
The object ID is set to null and uses the auto-increment number of the column ID in the custom table so I think it will not work when you use the custom model. I will inform the development team to check this case and get back to you later.
Thank, I look forward to hearing how to get the ID of the just added item.
Possible solution for the above issue...
public function insert_row( $row ) { global $wpdb; $id = isset( $row['ID'] ) ? $row['ID'] : null; do_action( 'mbct_before_add', $id, $this->table, $row ); $output = $wpdb->insert( $this->table, $row ); do_action( 'mbct_after_add', $wpdb->insert_id, $this->table, $row ); // Change $id to $wpdb->insert_id return $output; }