How to get last updated row or ID in wordpress.

Like : $wpdb->insert_id;

  • you have to use it after $wpdb->insert() is executed. Have a look here stackoverflow.com/questions/1576018/… – Piyush Rawat Nov 14 '17 at 4:27
  • I get last insert id.. but how I get last updated id... – KGR Nov 14 '17 at 4:30
  • In codeigniter $this->db->affected_rows that type in wordpress get the last affected_rows. It passable?. – KGR Nov 14 '17 at 4:36
  • i don't think there's a default function for it, what are you trying to update? – Piyush Rawat Nov 14 '17 at 4:38
  • ok.. I get the last affected id and insert in the another table.. that only I ask last affected id getting is passable?. – KGR Nov 14 '17 at 4:41

I had the same problem.

There is no equivalent function. $wpdb->insert_id works only after $wpdb->insert

Alternative way

If it's an update, you can either get the id with a query based on the data you have got, or most of the time, you should have already got the id before update it.

global $wpdb;

$data = array(
    'first_name' => "John",
    'last_name' => "Doe"
);

$where = array(
    'id' => $my_id
);

$res_update = $wpdb->update( $wpdb->prefix . "my_table", $data, $where );

if( $res_update === false ){
     error_log( 'my error');
}

return $my_id;

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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