skip to Main Content
a:1:{s:9:"image_url";a:2:{i:0;s:62:"http://localhost/wordpress/wp-content/uploads/2021/04/g4-5.jpg";i:1;s:62:"http://localhost/wordpress/wp-content/uploads/2021/04/g1-2.jpg";}}

Above is my data. I am using WordPress and I retrieve those data from post meta table. Now I want to show images only. So I want to retrieve image path which is store in above as S:62. I don’t know how I can retrieve. please help or guide me to solve my problem.

2

Answers


  1. It’s not JSON data it’s serialized data, if it is related to post meta data then you have to use WordPress function get_post_meta

    In WordPress, mostly you can find the serialized data in wp_options, wp_postmeta and wp_usermeta. WordPress provides various API to fetch these meta data from DB. (Here the table prefix wp_ is a configurable prefix so in most WP application you will find random prefix like df18ef_ this one change in WP DB will increase the security of you web application.)

    Don’t rely on custom SQL always try to use WP functions to fetch data because they optimize the fetching logic by cache the data in in-memory (especially transient data) object if your setup was configured for object caching then using WP built-in function save you a lot because most plugins used for object caching either using redis or memcache rely on WP provided hooks so instead of using SQL statement use WP functions to get proper data.

    Usage:

    $post_meta_1 = get_post_meta( get_the_ID(), 'post_meta_key', true );

    Login or Signup to reply.
  2. Use maybe_unserialize()

    $unserialize = maybe_unserialize( $serialize );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search