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

SELECT custom post type and its meta in SQL

Tom’s comment on the question makes a very important point: unless you are just using this SQL for diagnostic or exploratory purposes, there is almost definitively an easier and more efficient means to access this data, either through the WordPress APIs or via tools like WP CLI.

There is virtually no reason to use this SQL for logic within a WordPress plugin, theme, or site.

The way to think of SELECT... JOINs is that you are SELECTing from the “virtual table” produced by the JOIN.

We can SELECT all of the rows from the wp_postmeta table which have one of the meta_keys "honorific", "suffix", or "nickname" and a post_id field corresponding to the ID field in wp_posts rows with the post_type "employee" as such:

SELECT wp_postmeta.*
  FROM wp_posts INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
  WHERE wp_posts.post_type="employee"
    AND wp_postmeta.meta_key IN ("honorific", "suffix", "nickname");

Or more succinctly with table aliases,

SELECT m.*
  FROM wp_posts AS p INNER JOIN wp_postmeta AS m ON p.ID = m.post_id
  WHERE p.post_type="employee"
    AND m.meta_key IN ("honorific", "suffix", "nickname");

Related Posts:

  1. Export entries and multiple custom field meta to .csv?
  2. WordPress Orderby Numeric Value Not Working
  3. Show the same Article Available in Other Categories
  4. Post image in WordPress not appearing on home page
  5. Update Post Meta for a logged in user
  6. Two Custom Post Types Many to Many Relationship
  7. Update postmeta Parent when post_status child change
  8. Batch Extract Date from post title and put into ACF custom field
  9. Get_post_custom not fetching value from array wordpress
  10. Add a form in every post and save data in post meta
  11. Save data is post using php front end
  12. Show posts from WP Custom Post Type selected from a field in a metabox
  13. How to properly get the wp_postmeta.meta_value of a custom post type in specifics?
  14. update a posts of other custom post type
  15. Edit post meta direct from post.php?
  16. How to create content automatically when a post is published?
  17. Display a post count from a custom metabox selection
  18. Top 30 Songs using Custom Post Type
  19. Automatically add custom taxonomy when meta value gets to a set number or beyond
  20. I am having a problem with fetching product data in the Gutenberg block editor
  21. How to add post_type=value when editing that post type in the WordPress admin?
  22. Insert Multiple Post with Same Publish Button
  23. Adding dropdown select meta box to custom post type – seems restAPI is interfering
  24. Open Custom Post Type as PDF
  25. Update CPT post meta with update_post_meta and cron job
  26. Using The REST API How To Pull All Custom Posts?
  27. Custom meta box data array: foreach not working correctly?
  28. Undefined Variable – Custom Post Type Meta
  29. How i can add ‘N’ page of ‘N’ pages under posts loop?
  30. Ordering posts in Search & taxonomy by post_meta
  31. Insert post metadata for all posts in CPT at once if metadata no existent
  32. Display custom post type from dynamic custom field
  33. how to delete all users and posts based on ‘user_meta’?
  34. Getting posts under the custom post type ui category
  35. Populate dropdown from one custom post type inside another custom post type
  36. Storing posts from query and accessing later via AJAX call
  37. If post has custom field then display css-class
  38. Meta Key Value in current-user-only loop
  39. How to check if user meta field is empty in conditional else statement
  40. Display only one post each WEEK
  41. How to set YouTube video as featured image?
  42. List children on child post
  43. Notice: Undefined index: error and understanding wordpress
  44. How to Mass Delete Images from Media Library
  45. Restrict custom post type from appearing with ?post_types=
  46. Show custom post type filtered by category
  47. Custom Post Types. Are there any disadvantages/advantages in using a plugin to develop them?
  48. create parent post using wp_insert_post
  49. Turn on and off custom post type from admin?
  50. Missing Posts in Custom Taxonomy List
  51. Get posts between custom dates
  52. Display random posts, but omit the post it is on?
  53. How do I do this with WordPress? Taxonomies?
  54. Disable block with taxonomies at post page
  55. How to implement a Google map store locator
  56. I would like to have different styles for my posts based on the content of each post
  57. How can i add thumbnails images to particular post (using code not admin pannel) in wordpress
  58. Showing posts from different categories and from custom post type
  59. Get latest 3 posts from multiple CPT in one query
  60. Deleting Custom Posts & Meta Data on Uninstall
  61. Widget: Custom Post Type Post Listing Dropdown on Admin Side
  62. Allow non-logged in users to see a future post after clicking on a list of future posts
  63. Which post does a taxonomy term belongs to?
  64. List custom taxonomy specific to one custom post type
  65. How to add a custom-post-type post within another custom-post-type post edit screen using AJAX?
  66. Meta query for custom post type ignored in main query
  67. Changing custom type name hides the posts
  68. How do I create an archive page as a including metadata?
  69. Display Ad on Specific Categories
  70. Hiding posts by other users and non-logged in
  71. changing meta value and meta key of price field
  72. Posting to a Custom Post Type from front end – user generated content
  73. Getting meta in editor plugin, and event triggering issue
  74. Add custom field to Posts and sort by it
  75. How to duplicate entire custom post type
  76. Add custom post type settings to wordress default posts
  77. A better way to add a meta box to custom post types
  78. Restrict Access to Posts based on Custom User and Post Meta Data
  79. Get related posts of child term of custom post type
  80. Problem with displaying posts in the CPT category
  81. get_post_meta not working on publishing
  82. Should wp_postmeta meta_key always be unique for a given post_id?
  83. JS innerhtml changing style when using AJAX
  84. Show titles, date of all posts on single category page
  85. how can I register a post_meta field in an existing CPT and then call it again with get_post_custom()?
  86. get_post_meta returning no value in custom post type alert email
  87. Including metaboxes from custom post types in global search — continued
  88. Side effects of Script and Iframe in post
  89. post meta parameter in post custom-post-type endpoint with restapi
  90. How do I set all of a particular post meta to a value within the custom post type I’m in?
  91. How to change the post type a theme shows by default?
  92. Set a checkmark in a category based on a URL-parameter
  93. Several post types on WP Query by tag and taxonomy
  94. No Permission to add new Page, Post or CPT with Admin role
  95. Metabox not show in categories custom post type cmb2
  96. custom post type category count shortcode
  97. WordPress request fiter order by related post’s post_title
  98. Portfolio Page for Classic Posts
  99. Add post location with mile radius allowing search
  100. Get the category from custom post type
Categories custom-post-types Tags custom-post-types, post-meta, posts, sql
How to create a link-less parent (top level) menu item that doesn’t use an anchor tag
Initializing the cropper for image uploads

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