Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve wordpress posts with featured image via SQL

I'm using this query in a PHP script outside Wordpress to retrieve entries with their featured images

SELECT  ( SELECT guid FROM wp_posts WHERE id = m.meta_value ) AS url 
FROM wp_posts p, wp_postmeta m
WHERE p.post_type =  'post'
AND p.post_status =  'publish'
AND p.id = m.post_id
AND m.meta_key =  '_thumbnail_id'

...and it works fine.

But this way I get full-size image URL. I need to retrieve 'medium' or 'thumbnail' sizes of these images.

¿Any way to achieve this?

like image 343
John Smith Avatar asked Sep 28 '13 13:09

John Smith


People also ask

How do I fetch featured image in WordPress?

To add a featured image in a WordPress post, simply edit or create a new blog post. In the content editor, you'll find the featured image tab in the right column. You need to click on the 'Set Featured Image' area, and this will bring up the WordPress media uploader popup.

Where are WordPress featured images stored?

The specific folder where the image files are stored in WordPress is called the uploads folder located inside the /wp-content/ folder. Inside the uploads folder, your media files are stored by year and month folders. Additionally, you'll also see folders created by your WordPress plugins to save other uploads.

How do I display images in SQL database?

How to view images stored in your database Start SQL Image Viewer and connect to your database. For SQL Server databases, tables containing blob columns will be highlighted in green in the list of database objects. Write the query to retrieve your images, and execute the query.

Do we see featured images in WordPress posts?

Featured images are tucked away in the sidebar of the WordPress blog post editor, yet are a key option. You can assign a primary image for each post, both to act as a header and to represent it on archive and search pages. Adding and optimizing featured images is a vital task on just about any WordPress blog.


2 Answers

here is the response :

SELECT TITRE,DESCR,URL, CONCAT(LEFT(IMG, LENGTH(IMG) - LOCATE('.', 
REVERSE(IMG))),'-150x150.',SUBSTRING_INDEX(IMG, '.', -1)) AS IMG FROM (
SELECT    
p.`post_title` AS TITRE, 
(SELECT `meta_value` FROM wp_postmeta WHERE `post_id` = p.`ID` and `meta_key`='_yoast_wpseo_metadesc') AS DESCR,
p.`guid` AS URL,
(SELECT `guid` FROM wp_posts WHERE id = m.meta_value) AS IMG
FROM wp_posts p, wp_postmeta m
WHERE p.post_type =  'post'
AND p.post_status =  'publish'
AND p.id = m.post_id
AND m.meta_key =  '_thumbnail_id') TT

where DESCR is not null
like image 183
user1942990 Avatar answered Oct 27 '22 08:10

user1942990


The following query, adapted from the above, solved my particular problem which was simply to grab the last four posts and their featured images. Plus the post_name from which I could construct a pretty URL

SELECT title, post_name, date, content, CONCAT(LEFT(image, LENGTH(image) - LOCATE('.', REVERSE(image))),'-150x150.',SUBSTRING_INDEX(image, '.', -1)) AS image
FROM (
  SELECT    
  p.post_title AS title, 
  p.post_status AS 'status', 
  p.post_date AS date,
  p.post_content AS content,
  p.post_name AS post_name,
  (SELECT `guid` FROM wp_posts WHERE id = m.meta_value) AS image
  FROM wp_posts p, wp_postmeta m
  WHERE p.post_type = 'post'
  AND p.post_status = 'publish'
  AND p.id = m.post_id
  AND m.meta_key = '_thumbnail_id'
  ORDER BY date DESC
  LIMIT 4
) TT

Of course from there it's easy to make an excerpt etc using:

for($i=0; $i< $num_rows; $i++){  
  $post_content = mysql_result($query_result, $i, "content"); 
  $post_excerpt = substr($post_content, 0, 90); 
  $post_permalink = $post_url . mysql_result($query_result, $i, "post_name");

  echo $post_permalink; //etc

}
like image 2
mayersdesign Avatar answered Oct 27 '22 08:10

mayersdesign