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. Deleting WordPress posts and media files based on author via MYSQL database
  31. MySQL Error: : ‘Access denied for user ‘root’@’localhost’
  32. Rename a table in MySQL
  33. How should I tackle –secure-file-priv in MySQL?
  34. Can’t connect to MySQL server on ‘127.0.0.1’ (10061) (2003)
  35. Error Code: 2013. Lost connection to MySQL server during query
  36. WPDB Insert or if exists Update
  37. Checking if Database Table exists
  38. The MySQL alternatives: Do Percona Server and MariaDB work well with WordPress, and do they make WordPress go better?
  39. How To Export/Import WordPress (MySQL) Database Properly Via Command-Line?
  40. How to define composite keys with dbDelta()
  41. Is it possible to define two databases for one installation?
  42. importing data from non-wordpress mysql db
  43. “MySQL server has gone away” since update to 3.8
  44. Error establishing a database connection – with Debug Data
  45. Can I transfer a mysql database to another site?
  46. Database with mixed collation (utf8mb4 & utf8_general_ci)
  47. MySQL Syntax Error upon restoring database from backup [closed]
  48. Help running a MySQL query to update all wp_#_options tables in a Multisite install
  49. Using table lock with wp_insert_post?
  50. Is it safe to convert tables from MyISAM to InnoDB?
  51. Migrating database / content of non-CMS site to WordPress
  52. How do I properly update the WordPress database password?
  53. Adding new row to wp_post table
  54. How do I find which articles are missing a featured image in The WordPress database?
  55. WordPress Install and Database on separate hosting?
  56. Using GROUP CONCAT in my-sql query with wp_usermeta table
  57. Routine to convert custom post meta from old to new value
  58. Setup private content for specific users with daily updates [closed]
  59. Migrating from PDO using SQLite to clean new install using MySQL
  60. Which data is written and stored in the database when read-only (non-posting, non-commenting) users visit the site?
  61. Duplicated site isn’t recognized as a site
  62. Does post_meta data need to be unserialzed?
  63. Changing root password in PHPMyAdmin for WordPress Database when going live
  64. How to properly check if a table exists in WordPress Database using Show Tables Query
  65. How and where is wordpress adding mySQL content to database?
  66. Using AND and bracket grouping in SQL not working
  67. Is my way to change WordPress server is correct?
  68. How to delete a particular row in a database table
  69. Files on Localhost, Database on Server
  70. Remote database -> massive response time increase?
  71. Custom Post Type not showing anything added by code anymore?
  72. Getting “Error Establishing a Database Connection” on localhost in new Mac OSX Lion environment
  73. custom tables in wordpress Database? (can i just create them with sql?)
  74. Accidently changed the GUID
  75. Replacing javascript link in WordPress database without getting mysql syntax errors
  76. How to edit custom table data in frontend
  77. local wordpress broke after changing URL
  78. how to access 2nd Mysqli Database from different server for wordpress
  79. Error establishing a database connection – WordPress setup, everything else is working
  80. Help posting values to DB on submit using $wpdb->query
  81. How to map data on CSV to web pages?
  82. wpdb query to insert images in to post/page gallery
  83. Database structure for thousands of posts
  84. Search and Replace in Windows XAMPP site
  85. The function is deprecated; use mysql_real_escape_string() instead
  86. WPCLI search and replace in a particlar site dir effect another site-dir
  87. How can i syn wordpress user table with a stand-alone php script?
  88. insert data from a form:: Warning Empty query mysqli::query()
  89. How to get specific table by current user login
  90. $wpdb not working
  91. WordPress Database Posts Table query
  92. Hang Up Followed By Can’t select database
  93. Mysql Queries per Visit – Crazy High
  94. using same mysql user with many databases
  95. Why are my WordPress post queries so slow?
  96. Weird WP -Cli Error Connection Refused
  97. How to find fresh (last modified) and sql backup?
  98. Understanding Redundant postmeta Tables in WordPress Database
  99. Database table prefix different between wp-config.php and in database
  100. Naming conventions for database tables
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
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 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