Solved

How can I query a specific Wordpress Featured Image Size with SQL

Posted on 2015-01-23
1
405 Views
Last Modified: 2015-02-13
I guess my theme creates multiple featured image sizes.  I would like to query the "medium" image size from the meta_value in the post_meta table.

a:6:{s:5:"width";i:900;s:6:"height";i:500;s:4:"file";s:49:"2015/01/blog_lego_smaller_boxes_green_profits.jpg";s:5:"sizes";a:5:{s:9:"thumbnail";a:5:{s:4:"file";s:49:"blog_lego_smaller_boxes_green_profits-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";s:10:"wp_smushit";s:28:"Reduced by 6.7% (599 B)";}s:6:"medium";a:5:{s:4:"file";s:49:"blog_lego_smaller_boxes_green_profits-300x167.jpg";s:5:"width";i:300;s:6:"height";i:167;s:9:"mime-type";s:10:"image/jpeg";s:10:"wp_smushit";s:29:"Reduced by 7.5% (1.3 KB)";}s:14:"shop_thumbnail";a:5:{s:4:"file";s:49:"blog_lego_smaller_boxes_green_profits-200x200.jpg";s:5:"width";i:200;s:6:"height";i:200;s:9:"mime-type";s:10:"image/jpeg";s:10:"wp_smushit";s:28:"Reduced by 6.6% (897 B)";}s:12:"shop_catalog";a:5:{s:4:"file";s:49:"blog_lego_smaller_boxes_green_profits-400x400.jpg";s:5:"width";i:400;s:6:"height";i:400;s:9:"mime-type";s:10:"image/jpeg";s:10:"wp_smushit";s:29:"Reduced by 6.8% (2.3 KB)";}s:11:"shop_single";a:5:{s:4:"file";s:49:"blog_lego_smaller_boxes_green_profits-600x500.jpg";s:5:"width";i:600;s:6:"height";i:500;s:9:"mime-type";s:10:"image/jpeg";s:10:"wp_smushit";s:29:"Reduced by 7.1% (4.1 KB)";}}s:10:"image_meta";a:11:{s:8:"aperture";i:0;s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";i:0;s:9:"copyright";s:0:"";s:12:"focal_length";i:0;s:3:"iso";i:0;s:13:"shutter_speed";i:0;s:5:"title";s:0:"";s:11:"orientation";i:0;}s:10:"wp_smushit";s:27:"Reduced by 1.8% (1 KB)";}

Open in new window


I am looking to grab this:

s:6:"medium";a:5:{s:4:"file";s:49:"blog_lego_smaller_boxes_green_profits-300x167.jpg"

Open in new window


in the end i will just get this

blog_lego_smaller_boxes_green_profits-300x167.jpg

Open in new window



Is there a way I can query the db to return that image name?
0
Comment
Question by:theideabulb
1 Comment
 
LVL 35

Accepted Solution

by:
Kimputer earned 500 total points
ID: 40566562
Can you just query the usual (get the whole string)?

Then put it through here (of course, replace $test with the result string from the database):


<?php

$test =  'a:6:{s:5:"width";i:900;s:6:"height";i:500;s:4:"file";s:49:"2015/01/blog_lego_smaller_boxes_green_profits.jpg";s:5:"sizes";a:5:{s:9:"thumbnail";a:5:{s:4:"file";s:49:"blog_lego_smaller_boxes_green_profits-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";s:10:"wp_smushit";s:28:"Reduced by 6.7% (599&nbsp;B)";}s:6:"medium";a:5:{s:4:"file";s:49:"blog_lego_smaller_boxes_green_profits-300x167.jpg";s:5:"width";i:300;s:6:"height";i:167;s:9:"mime-type";s:10:"image/jpeg";s:10:"wp_smushit";s:29:"Reduced by 7.5% (1.3&nbsp;KB)";}s:14:"shop_thumbnail";a:5:{s:4:"file";s:49:"blog_lego_smaller_boxes_green_profits-200x200.jpg";s:5:"width";i:200;s:6:"height";i:200;s:9:"mime-type";s:10:"image/jpeg";s:10:"wp_smushit";s:28:"Reduced by 6.6% (897&nbsp;B)";}s:12:"shop_catalog";a:5:{s:4:"file";s:49:"blog_lego_smaller_boxes_green_profits-400x400.jpg";s:5:"width";i:400;s:6:"height";i:400;s:9:"mime-type";s:10:"image/jpeg";s:10:"wp_smushit";s:29:"Reduced by 6.8% (2.3&nbsp;KB)";}s:11:"shop_single";a:5:{s:4:"file";s:49:"blog_lego_smaller_boxes_green_profits-600x500.jpg";s:5:"width";i:600;s:6:"height";i:500;s:9:"mime-type";s:10:"image/jpeg";s:10:"wp_smushit";s:29:"Reduced by 7.1% (4.1&nbsp;KB)";}}s:10:"image_meta";a:11:{s:8:"aperture";i:0;s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";i:0;s:9:"copyright";s:0:"";s:12:"focal_length";i:0;s:3:"iso";i:0;s:13:"shutter_speed";i:0;s:5:"title";s:0:"";s:11:"orientation";i:0;}s:10:"wp_smushit";s:27:"Reduced by 1.8% (1&nbsp;KB)";}';

$pattern = "#\"(.*?)\"#";
preg_match_all($pattern, $test, $matches);

$key = array_search('medium', $matches[1]);

print_r($matches[1][$key+2]);

?>

Open in new window

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question