WordPress get id from json data with json_decode()

json_encode takes an array/object and turns it into a JSON string.

json_decode takes a JSON string and turns it into a PHP array/object/etc

With this in mind, we can see some critical mistakes are made in the code, caused by muddling up the two.

  1. You’re taking a string of JSON and JSON encoding it, aka double JSON encoding it, which doesn’t make sense. Think of it like doing this: json_encode( json_encode( ['ID' => 2] ) )
  2. You’re then immediatley decoding it, so $post_decode and $post->title are the same, it’s like turning on the light switch then turning it off everytime you go past, it’s a non-operation, nothing is achieved
  3. Then, for some reason json_encode is called, but it’s called on a nonexistant value. Remember, $post_decode is not an array/object, it’s a JSON string that hasn’t been decoded, what you’re doing is the same as $foo = 'bar'; echo $foo->ID;. Expect this to generate huge amounts of PHP notices and warnings in your PHP error log.

This is all very confusing and unnecessary.

Instead take your post title and decode it from JSON into an object:

$data = json_decode( $post->post_title );

Then use it:

$ID = $data->id;

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)