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

Querying Database with wpdb

I tested your all attempts in a shortcode:

function add_test_shortcode() {
  global $wpdb;
  global $post;
  $post_ID = $post->ID;
  $wpdb->show_errors = true;
  $result = $wpdb->get_var('SELECT meta_value FROM '.$wpdb->postmeta.' WHERE meta_key="my_test" AND post_id=' . $post_ID);

  return $result;
}
add_shortcode( 'test', 'add_test_shortcode' );

It worked perfectly for me, so there might something from your own code:

1. Check for $post_ID

Make sure your $post_ID is valid, get_the_ID() might be a better replacement in so many cases than global $post;.

2. Test your generated query

You can get your last executed query using $wpdb->last_query, execute it in phpMyAdmin or any other software and check that there is really a result.

3. Look if there’s any error

You can get the latest error statement using $wpdb->last_error.

My advice to you during WordPress development is that always keep WP_DEBUG on.

Related Posts:

  1. how to get serialized post meta
  2. Order by in foreach
  3. $wpdb->insert() does not Insert record in a table
  4. Help with a $wpdb MySQL Query
  5. Fatal error: Call to a member function query() on a non-object
  6. Is it possible to retrieve all posts with a certain value for metadata?
  7. Get post id within comments loop
  8. Non-Closing PHP Query in WordPress Loop
  9. Custom array from a query only write the last row of the query
  10. Error using WordPress $wpdb object to get result from custom table
  11. How to retrieve the data from the sever and displaying it in a page?
  12. $wpdb returns duplicate posts
  13. Can’t update multiple rows with $wpdb query
  14. Mixing variables into an array when inserting values
  15. update_post_meta only updating on last loop of foreach
  16. Wpdb->insert() doesn’t insert new row after the last one
  17. How to use two meta_compare in an array?
  18. Use $wpdb or other PHP script method to find/replace in WP database
  19. How can I add a new row in a separate database when someone registers via WordPress?
  20. How to get specific attribute from DB
  21. Basic wpdb update question
  22. Remove empty terms from array, sort alphabetically, update back to repeating field
  23. WordPress loop by meta key that is an array? and how loop multiple arrays
  24. I can’t update my data through $wpdb
  25. WordPress yoast seo plugin – parse snippet var in meta description
  26. wpdb Cannot Update column in Database
  27. Output: “Array”
  28. How to use mysql LIKE with wpdb?
  29. How to store multiple custom meta box
  30. PHP -> SQL Query with Summing
  31. How to insert wp_users ->user login name to wp_terms when a new user registering?
  32. Delete database record using plugin from admin panel
  33. Cannot access $wpdb, comes back NULL
  34. Wpdb get->results to out the the month from the db
  35. How to add post meta in while loop?
  36. Add category to post meta
  37. How do I prepare strings for insertions as values into a MySQL table?
  38. Using rules in Posts
  39. How to use AJAX in WordPress in MYSQL query?
  40. Update post meta – Custom field does not match meta-key
  41. Change MySQL PDO connection to a WPDB connection
  42. WPDB – Read and write value from / to database
  43. Order a WP_Query by meta value where the value is an array
  44. Save the value of a wp_dropdown_pages
  45. $wpdb->get_results breaking page?
  46. Add a custom class to the body tag using custom fields
  47. form $_post action value gets truncated after it passes through two forms
  48. Why my query does not run with prepare
  49. Define category ID using get_post_meta
  50. How to Generate a list of Most Commented post?
  51. Why won’t this wpdb get_results query return results?
  52. WPDB secure custom form
  53. Unable to Call More than One Meta Box Output
  54. wpdb getting avatars and author url
  55. Hide a div when a custom field is empty
  56. Undefined variable: row?
  57. Update value of a associative array with update_post_meta
  58. Add row to custom database Table and delete all rows older than 1 day
  59. Front end file upload returning wrong attachment url
  60. Call to a member function get_row() on a non-object, Unable to use global $wbdp inside a plugin
  61. Post meta select input, if statement
  62. select a single val though a table in wordpress
  63. WordPress SQL JOIN query
  64. populate select options from extra mysql table data
  65. Query the links Database
  66. Fetching review value using wpdb class
  67. WordPress stripping out custom field tags
  68. MySQL Query Returns Array () In Shortcode
  69. WordPress get last inserted user id
  70. How to check post meta isset or not empty during publish/save post?
  71. Add posts to custom table in database instead of wp_posts
  72. Isn’t Returning Value While Using SELECT COUNT(*) FROM {$wpdb->prefix}
  73. get value from get_post_meta then reuse it in another get_post_meta
  74. Passing in MySQL prepare statement parameter separately throwing error
  75. Convert a column of a table containing an Array as response in HTML
  76. Order category posts by last word of custom field
  77. Database SQL query error
  78. WordPress SQL Prepare
  79. Add a “custom field” to a category that can be retrieved when viewing the category page with get_post_meta
  80. Loop for recently (updated_post_meta) posts?
  81. How to add and subtract user meta values after post meta update
  82. How to update custom fields when post is published?
  83. Update Images after edit via php
  84. Creating multiple tables with Plugin
  85. MYSQL TIMESTAMP when adding DATE_FORMAT then the output is blank, PHP conflict?
  86. No more get_option(‘date_time’) in WordPress 5.5?
  87. Conditional formatting on data fetched from MYSQL
  88. Delete post meta by serialized meta value
  89. Using zip code to display custom data in Admin Order Details
  90. What is the correct way to search 3 custom fields only in WordPress?
  91. Using wpdb to connect to a different database is not working
  92. Show a text if the post has written by author user role in WordPress
  93. Looping through custom data in a custom table to display all items in a post
  94. Hide author from search results meta, still display date and categories
  95. Fix common misspellings/errors based on array of replacements
  96. Displaying SQL query result from user input via wpdb
  97. update_post_meta Not Processing Array Data (Not Sure What I Am Missing)
  98. How do I create a WP endpoint that retrieves all custom post types that have a tag?
  99. Modify post image in full size
  100. Unable to update the meta boxes with multiple fields
Categories PHP Tags php, post-meta, wpdb
Create an archive page for custom post type with custom taxonomy
Echo get_the_category() outside of loop (global?)

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