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

get_post_meta for Custom Post Type ( CPT )

Okay, so long story short… there’s no built-in wordpress way to Query the meta-data directly, but in the interests of helping others, essentially you need to do it the old-fashioned way… using SQL:

I’m going to do up a post which I’ll link to… but essentially it involves straight SQL and $wpdb->get_results and some PHP loops.

To get all the values for _ns_research_document, _ns_monograph_document, and _ns_profile_document I did this:

// prepare SQL
$wpdb->prepare("SELECT DISTINCT post_id, meta_key, meta_value"
 ." FROM " $wpdb->postmeta 
 ." WHERE meta_key IN ( '_ns_research_document', '_ns_monograph_document', '_ns_profile_document') "
 ." ORDER BY post_id, meta_key ASC" );

// call DB
$results = $wpdb->get_results( $sql );

It returns an array with an array for each row, which I was able to loop through and recategorize. Kind of surprising I couldn’t find a WP way to do this without SQL, but such is life.

Note that if you wanted to be sure to ONLY get product posts, you’d probably have to do an INNER JOIN in your sql.

Related Posts:

  1. Create a random unique 6 digit number as custom field for custom post type
  2. Very Slow Page – How to Optimize # of Queries?
  3. How to make sure content doesn’t display if selection is empty
  4. How can I group posts by months and years?
  5. Loop increase number
  6. Using wp_query to modify the loop in index.php for a CPT
  7. foreach loop inside the loop creating duplicates in output
  8. How to retrive Custom Post Type Meta Fields in Custom WP_Query
  9. Page that lists publications by classifying them by taxonomy
  10. Custom meta fields not showing up in WP_Response Object via custom endpoint
  11. WordPress loop add heading before first of type
  12. How to make a shortcode for my WP_Query Loop? [duplicate]
  13. Strange behavior on WP_query
  14. Values inside a custom field to determine which category posts to display
  15. Query Custom Post Type by Tag
  16. Separate Posts and Custom Post Type in Custom Taxonomy archive template
  17. Displaying custom posts only if custom meta box’s date is not expired
  18. Impossible to get Attachments Outside WordPress?
  19. WP_Query search posts by custom post type and custom taxonomy
  20. Why does using WP_Query inside a shortcode in an elementor page cause the arguments for WP_Query to get malformed?
  21. Unable to get post content from custom post type loop
  22. Lists Posts from CPT that match the Title of the Page
  23. Help Structuring Query for Archive Pages
  24. Display custom field of specific post where post title matches variable
  25. Conditional to modify query results
  26. Loop on front-page.php
  27. get_template_part() – post-meta not working?
  28. wp_query to find posts by year and month
  29. WP_Query is printing out only one post when posts_per_page is set to multiple
  30. WP Query to displaying date and posts for that date?
  31. Remove duplicated values from a loop
  32. Custom Post Type Taxonomies -Posts not showing in Category or Tag pages
  33. WordPress Orderby Numeric Value Not Working
  34. Custom Post Type Loop within Shortcode
  35. Can’t get order_by meta_value_num to work properly
  36. WP_Query | ‘post_type’ doesn’t work
  37. How to connect two custom post types with nested loops
  38. Filter by custom taxonomy slug on a custom post type
  39. Query custom post type in the loop
  40. Taxonomy Archive: Display only one post per term from separate custom taxonomy
  41. Loop through Custom Post Type, and then show children within each iteration
  42. orderby in custom WP Query does not work
  43. Can I create a loop with multiple post types and specify different $args for each post type?
  44. Display CPT posts based on specific taxonomy
  45. WP_Query orderby modified to include custom meta changes
  46. Custom Post Type Loop throws 500 error when used in widget
  47. Display custom post type from dynamic custom field
  48. Show one item per category of a custom post type
  49. Two near-identical custom field types – one works, the other doesn’t . What can cause this?
  50. Meta Key Value in current-user-only loop
  51. CPT + CMB2: data not displaying for only first post in loop
  52. How do i calculate the total of values of custom fields in custom post types?
  53. Custom loop with multiple taxonomy queries
  54. I would like to have different styles for my posts based on the content of each post
  55. Checking if Post Title is Unique as Loop Criteria
  56. Custom Tag Description unable to display just below and outside of the Loop
  57. get_post_meta not working on publishing
  58. creat filter with wp_query
  59. What’s the WP way to load remaining custom posts?
  60. WP Query with categories only shows one post and ignores the category
  61. WordPress request fiter order by related post’s post_title
  62. Problem: wp_query outputs all images on site
  63. Display post from custom post type
  64. List all Custom Post Type posts excluding certain Taxnomy term
  65. WP_Meta_Query object with conditionals
  66. Foreach loop returning more than one item when querying taxonomy
  67. Compare Two Custom Post Types Using The Same Custom Taxonomy
  68. How to fetch posts that are stored in the different table than (default) wp_posts?
  69. Cant’ Display Custom Post Type Title Base on Tax Terms
  70. Loop for custom-post-type comparing taxonomy terms for “related” posts?
  71. How do I list a custom field and custom taxonomies for each result in a loop?
  72. Custom Post Type Query W/Category Dropdown
  73. Multiple custom post type queries causing wrong post types to be grabbed in taxonomy + single templates?
  74. How can I increase the post count for custom post types only?
  75. Custom post type pagination error
  76. Query for specific taxonomy that executes a particular loop depending on volume of posts?
  77. Show the categories the current post has
  78. What is the most efficient way to execute recursive complex queries?
  79. Fetch data from two custom post types and create multidimensional array for output to html table
  80. CPT loop doesn’t seem to account for post date?
  81. posts_per_page in custom WP_Query does not override “Reading” settings?
  82. set object terms after some some time of published post – functions.php
  83. After inserting new post with wp_insert_post() the post is not visble to WP_Query, but the same WP_Query works for post inserted from wp-admin panel
  84. Alike Shortcode using in Custom Shortcode
  85. Query custom post type that has a serialized relational advanced custom field value
  86. Exclude a specific post in a Custom Post Type
  87. Pin posts to top of custom loop
  88. Pagination not working in custom post type. Help
  89. Sort custom post column by generated value?
  90. Filtering posts based on three taxonomies
  91. Pagination Not Working When Used With WP_Query() `offset` Property
  92. when looping through custom post data not appearing
  93. Order posts alphabetically with numbers but some of the posts has numbers in the title
  94. WP_Query – How To Query Only Custom Posts With No Children of Their Own?
  95. Use WP_query to match post types based on custom field values
  96. How to sort by multiple values in a nested WP_Query
  97. wp_post_delete is deleting all offices
  98. SEARCH QUERIES – REVERSE OUTPUT
  99. Accessing download link from the loop with WP Download Manager Pro
  100. Custom Post Type has wrong label and is not found when called by a loop
Categories custom-post-types Tags custom-post-types, loop, php, post-meta, wp-query
User custom meta delete by serialised key value
How to Modify My Plugin’s Admin Settings so that the Plugin can be Added to a Specific 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