Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

SQL query : Select a thumbnail for a media

$Featured_image = $wpdb->get_results("
    SELECT p.*
      FROM net_5_postmeta AS pm
     INNER JOIN net_5_posts AS p ON pm.meta_value=p.ID 
     WHERE pm.post_id = $da_id
       AND pm.meta_key = '_thumbnail_id' 
     ORDER BY p.post_date DESC 
     LIMIT 15
",'ARRAY_A');

A related solution, to query for posts WITHOUT providing a post ID (ordered by post date, and using the wp_ database prefix):

SELECT
    p1.*,
    wm2.meta_value
FROM 
    wp_posts p1
LEFT JOIN 
    wp_postmeta wm1
    ON (
        wm1.post_id = p1.id 
        AND wm1.meta_value IS NOT NULL
        AND wm1.meta_key = "_thumbnail_id"              
    )
LEFT JOIN 
    wp_postmeta wm2
    ON (
        wm1.meta_value = wm2.post_id
        AND wm2.meta_key = "_wp_attached_file"
        AND wm2.meta_value IS NOT NULL  
    )
WHERE
    p1.post_status="publish" 
    AND p1.post_type="post"
ORDER BY 
    p1.post_date DESC

Related Posts:

  1. WordPress and MySQL: how to transfer Meta_key and Meta_Value from one post_id to another
  2. Inserting Post Meta From SQL
  3. Migrating non-WordPress CMS to WordPress, lots of data to move — possible solutions?
  4. How to solve slow WordPress site caused by attachment_metadata
  5. Delete all post meta except featured image
  6. How to execute mulitple statement SQL queries using $wpdb->query?
  7. Delete duplicate rows from wordpress database where a column is duplicate in phpmyadmin
  8. Query WordPress database by registered date and role
  9. Size article thumbnails and retrieve them directly from database
  10. Delete all post meta except featured image Using SQL
  11. wpdb_prepare with multiple or condition
  12. SQL Error wp_commentmeta – incorrect table definition
  13. How do I get the posts within a certain year/date(with sql query)?
  14. Create table from array with prepare
  15. Why this query is not showing any result on wordpresss home page?
  16. Why does changing Database Table Prefixes result in loss of content?
  17. SQL query to set posts in bulk based on the post content
  18. $wpdb->get_var – What value is returned when zero records are found?
  19. DB prefix not updating
  20. I wanted to Know that how can I give ingore to 0000-00-00 in my config file
  21. “BS_” rows in postmeta table
  22. How to create index (sql) to a meta_key?
  23. Delete all users meta that named: user_avatar
  24. The MySQL Connection could not be established., before it was joomla but i have change it into worpress
  25. How do i get value from wp_postmeta?
  26. Migrated to Namecheap, now Trouble with Database & wp-config [closed]
  27. export individual posts to text files or a single csv file
  28. why you don’t Implement PDO for access every thing to database
  29. SQL Query to get post_id from wp_posts and and meta_key(s) from wp_postmeta
  30. #1067 – Invalid default value for ‘post_date’ when trying to create a new column
  31. Deleting WordPress posts and media files based on author via MYSQL database
  32. Deleting empty meta key values
  33. Sql query to get all metadata for a post
  34. Recovery – Restore Database after moving folder location locally
  35. Replacing javascript link in WordPress database without getting mysql syntax errors
  36. Performing CRUD operations on front end in wordpress
  37. I imported an restore an database and It shows #1067 – Invalid default value for ‘user_registered’
  38. Set a cron job to delete a specific database table table every midnight
  39. How to edit custom table data in frontend
  40. Job and Employee Performance Tracking with Product Safety
  41. local wordpress broke after changing URL
  42. how to access 2nd Mysqli Database from different server for wordpress
  43. Migrating meta_key and meta_value from old theme to new one
  44. Error establishing a database connection – WordPress setup, everything else is working
  45. How can I find user role in Mysql?
  46. SQL database import #1046 error
  47. Bulk find & replace on WordPress posts/pages (minus image paths)
  48. How to import products in host instead of database on wordpress?
  49. I would like some help wth an SQL query to link posts with categories
  50. How do I loop/iterate through posts to edit all img tags?
  51. Where is the HTML-handler part in the wpdb class?
  52. Exporting Post ID, Post Title, Primary Category and Primary Category ID
  53. Featured image from custom to WordPress database
  54. Help posting values to DB on submit using $wpdb->query
  55. What’s the easiest way to connect external database and display data on wordpress?
  56. WordPress Database Query works in phpMyAdmin but not in the code
  57. How to map data on CSV to web pages?
  58. Is there a way for two deferent themes to consume two different DB on a same WP instance?
  59. wpdb query to insert images in to post/page gallery
  60. How to set posts to draft in bulk based on the content of the post
  61. next comment id number wordpress
  62. Database structure for thousands of posts
  63. 2 $wpdb queries causing error Table ‘wp_postmeta’ is specified twice, both as a target for ‘UPDATE’
  64. Search and Replace in Windows XAMPP site
  65. The function is deprecated; use mysql_real_escape_string() instead
  66. WPCLI search and replace in a particlar site dir effect another site-dir
  67. How can i syn wordpress user table with a stand-alone php script?
  68. insert data from a form:: Warning Empty query mysqli::query()
  69. How can I get $wpdb to show MySQL warnings?
  70. Correct and secure way to access a custom SQL database in a custom PHP template file
  71. How to get specific table by current user login
  72. Merging development site with live site
  73. mySQL database size large, but tables size less
  74. How to retrieve user data based on role using SQL?
  75. spambot registering without providing email or password, bypassing registration process
  76. After renaming MySQL database, I can no longer access my Blog page
  77. $wpdb not working
  78. How To connect to the same WordPress database with different database user
  79. Possible to restore wordpress database from mysql56 and sqlite files?
  80. WordPress Database Posts Table query
  81. How much post meta data is too much?
  82. Hang Up Followed By Can’t select database
  83. WordPress character set issue
  84. How to extract some part of WordPress full source code
  85. can’t delete a row from post_meta table
  86. Mysql Queries per Visit – Crazy High
  87. Missing latest posts and options after DB migration via phpMyAdmin
  88. Check if a database is well installed
  89. Create a new table on the database and save parameters from a contact form
  90. live site do not update after importing sql database
  91. Link wordpress with a SQl + MongoDB + .NET
  92. Why is converting my database to UTF-8 truncating entries?
  93. Widgets with *internal* links do not display after database migration
  94. using same mysql user with many databases
  95. How to add data from wordpress tables to new database tables you have created
  96. “MySQL server has gone away” since update to 3.8
  97. Why are my WordPress post queries so slow?
  98. featured image not showing up with mysql_connect
  99. MySQL Database User: Which Privileges are needed?
  100. Import Excel (or csv) Table with user-data as subscribers to wordpress
Categories Database Tags database, gallery, mysql, post-meta, sql
Assign a link of a post to that contains that post?
How can I add different menus (different style menu) on each page?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress