phpmysqlwordpresspost-meta

How to extract data from WordPress's wp_postmeta table?


I have a WordPress website when some user fills in the form I want to find 3 users nearest to him and send them an email.

I don't think it's possible with WordPress without editing the theme and I don't have much experience with WordPress.

So I created a form with Custom PHP (not in WordPress) which has access to the WordPress database and I'm trying to select 3 users nearest to user location.

for the current user, I have zip code, city, address, and for other users, I have zip code, address, latitude, and longitude stored in the database which serialized by WordPress.

I have searched on how to do this and found the Haversine formula. but it needs to have latitude and longitude values.

How can I extract latitude and longitude from this wp_postmeta table to get the nearest users?

+------------------------------------------+
meta_id | post_id | meta_key | meta_value 
+------------------------------------------+
  12    |   25    | lp_options | a:32:{s:12:"tagline_text";s:36:"Legendary sushi maestro in your town";s:8:"gAddress";s:47:"86 East Pine Street, Seattle, WA, United States";s:8:"latitude";s:10:"47.6152846";s:9:"longitude";s:19:"-122.30498549999999";s:6:"mappin";s:0:"";s:5:"phone";s:14:"(206) 441-8844";s:8:"whatsapp";s:0:"";s:5:"email";s:27:"codewithdeveloper@gmail.com";s:7:"website";s:24:"http://sushikashiba.com/";s:7:"twitter";s:19:"https://example.com";s:8:"facebook";s:19:"https://example.com";s:8:"linkedin";s:19:"https://example.com";s:7:"youtube";s:19:"https://example.com";s:9:"instagram";s:19:"https://example.com";s:5:"video";s:43:"https://www.youtube.com/watch?v=oMxLKOv_3t0";s:7:"gallery";s:0:"";s:12:"price_status";s:14:"ultra_high_end";s:10:"list_price";s:0:"";s:13:"list_price_to";s:0:"";s:7:"Plan_id";s:1:"0";s:16:"lp_purchase_days";s:0:"";s:11:"reviews_ids";s:0:"";s:15:"claimed_section";s:11:"not_claimed";s:26:"listings_ads_purchase_date";s:0:"";s:30:"listings_ads_purchase_packages";s:0:"";s:4:"faqs";a:2:{s:3:"faq";a:2:{i:1;s:11:"Specialties";i:2;s:7:"History";}s:6:"faqans";a:2:{i:1;s:153:"Legendary sushi maestro, Chef Shiro Kashiba brings his authentic and innovative Edomae style of sushi and acclaimed Japanese cuisine to downtown Seattle.";i:2;s:226:"Chef Shiro Kashiba started Seattle's first sushi bar in 1970 after years of grueling training alongside world renown sushi chef Jiro Ohno in Tokyo. Chef Shiro Kashiba introduced his masterpiece, Sushi Kashiba in December 2015.";}}s:14:"business_hours";a:6:{s:6:"Monday";a:2:{s:4:"open";s:7:"02:00pm";s:5:"close";s:7:"11:00pm";}s:7:"Tuesday";a:2:{s:4:"open";s:7:"02:00pm";s:5:"close";s:7:"11:00pm";}s:9:"Wednesday";a:2:{s:4:"open";s:7:"02:00pm";s:5:"close";s:7:"11:00pm";}s:8:"Thursday";a:2:{s:4:"open";s:7:"02:00pm";s:5:"close";s:7:"11:00pm";}s:8:"Saturday";a:2:{s:4:"open";s:7:"02:00pm";s:5:"close";s:7:"11:00pm";}s:6:"Sunday";a:2:{s:4:"open";s:7:"02:00pm";s:5:"close";s:7:"11:00pm";}}s:11:"campaign_id";i:93;s:14:"changed_planid";s:0:"";s:19:"listing_reported_by";s:0:"";s:16:"listing_reported";s:0:"";s:13:"business_logo";s:0:"";

Solution

  • WordPress uses php's serialize() / unserialize() stuff to encode the data arrays it puts into its wp_postmeta table. That table holds key / value pairs for arbitrary metadata for each post on the instance.

    The coding format is conceptually similar to JSON, but syntactically different.

    $lpOptions = unserialize( $thatStringStarting_a_32 );
    

    gets you an array with elements like

    $lpOptions['tagline_text']
    $lpOptions['latitude']
    $lpOptions['longitude']
    

    and others. Careful: unserialize can mess you up if a cybercreep sends you maliciously crafted data strings.

    Are you programming this in a WordPress module (plugin, template)? If so, please use the WordPress Core get_post_meta() function to get this data from MySQL.

    $lpOptions = get_post_meta( $post_id, 'lp_options' );