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

Custom MySQL Query for Post and Post Meta

You have to use JOIN for that. Try it like this:

$wpdb->get_results("SELECT tags.*, $wpdb->posts.*, $wpdb->postmeta.*
              FROM tags
              INNER JOIN $wpdb->posts ON tags.charity_id = $wpdb->posts.ID
              INNER JOIN $wpdb->postmeta ON tags.charity_id = $wpdb->postmeta.post_id
              WHERE tags.charity_id = $charity_id"
            );

Related Posts:

  1. Grouping related postmeta data via SQL query
  2. mySQL query. ORDER BY meta_key
  3. simple sql query on wp_postmeta very slow
  4. How many WordPress SQL Queries per page?
  5. Search custom post type by meta data
  6. Multipart/formatted MySQL query problem
  7. How to display liked posts of current user in wordpress?
  8. Filter query posts by the count of metas for a meta key
  9. Are database queries created using WordPress filters protected from SQL injection?
  10. Save default value for empty or missing post meta
  11. Mysql / WordPress killing my server with 80k users [closed]
  12. Slow meta_query with about 4 milion record on wp_postmeta
  13. Select User by Joining Multiple Meta Value Results
  14. Remove posts from query for events whose start date has passed
  15. Update slug (URL) of pending posts via phpMyAdmin
  16. My SQL function to change user_name
  17. Query metas (and not : query posts by metas)
  18. You have an error in your SQL syntax – Help with query
  19. MySQL variable in query
  20. how would I create a custom query to get all users, and a related post based on a postmeta field?
  21. Help with MySQL to $WPDB query
  22. SQL query to select posts from multiple categories
  23. How do I see the mysql query generated by get_posts( $args )?
  24. Alter query with posts_clauses to retrieve NULL values last
  25. Issue with using add_rewrite_rule() for post querying custom fields
  26. MySql Query very slow
  27. Searching With Apostrophe
  28. WordPress Query wp-terms SLOW
  29. Query posts according to specific post meta values
  30. wpdb COALESCE won’t work
  31. Order by meta value pro first then meta value free in my search function
  32. Get posts from category with custom query
  33. how to insert missing tags into the posts through mySQL?
  34. Ideas how to search & replace post_content when string contains a newline?
  35. Valid SQL query return empty
  36. mysql query – how to escape apostrophe?
  37. Query the WordPress database to get data together with replaced information
  38. Optimize slow SQL query for multiple meta values
  39. Updating with $qpdb->query() always returns 0 rows affected
  40. get_post_meta with WP_query
  41. Declaring a var, placing it in a query and using the output of the query?
  42. How to return count of custom post type posts with a specific custom field value via $wpdb?
  43. WordPress Mysql query and Duplicate
  44. Help with Related Posts Function
  45. Combining two meta_values within one row with query
  46. How to delete all images from code in all post_content
  47. I want to get on those users their meta value are like “AGENT” .. but this query is not working
  48. Displaying data from custom table
  49. Get 2 meta values from meta key column
  50. PHP Fatal error: Uncaught Error: Call to a member function insert() on null
  51. SQL query to delete users with multiple meta keys and comments
  52. Is there any way to get all custom posts and all custom terms with it’s meta in few queries?
  53. Sort by postmeta on when searching
  54. OR condition not working
  55. Trouble migrating custom post types from non-wordpress cms
  56. Are there any best practices for creating a Like/Favourite feature in WordPress using custom MySQL tables and without any plugins?
  57. Performance of wp_get_attachment_image_srcset() and wp_get_attachment_image_url()
  58. How to make MySQL search queries with quotes
  59. how to get postmeta info based on multiple categories?
  60. Use $wpdb->get_results with filter based on array
  61. What is the best way to query posts based on live data?
  62. Mysql query and odd results
  63. sql for querying post and their category
  64. Access Tables with number prefix
  65. is it possible to get a list of URLs from post_content directly in a mysql query via phpadmin?
  66. Custom API how to return int field
  67. Problem in MySql query on WordPress [closed]
  68. SQL Query : how copy all tags of post into their post content in wordpress by sql query
  69. If meta_key exists then don’t select this post?
  70. Print data from wordpress sql query
  71. How to add multiple values with add_query_arg?
  72. Delete query won’t run
  73. List of buyers for each product
  74. Where can I find the SQL to get the most used information by wordpress database?
  75. query posts only works on the first page
  76. Custom Query for wp_posts using wp_postmeta
  77. Query parsing only author ids
  78. WooCommerce Total # orders [closed]
  79. What is the best way to cache pages and queries in WordPress?
  80. 3 queries to update WordPress
  81. WP Query outputs three items within a div
  82. SQL Query to get post_id from wp_posts and and meta_key(s) from wp_postmeta
  83. Can I create my own query in wordpress with traditional methods?
  84. Modify a query using both a text field and a true/false AFC field
  85. How do I troubleshoot Maximum execution time of 60 seconds exceeded in updating a Media File?
  86. Query filter on custom post by current author in post widget for elementor query id
  87. update $wpdb one query
  88. Admin Post List table Query filtering “WHERE” for custom post type
  89. Ascending & descending posts on same page with IF statement
  90. Prevent executing query for wp_global_styles
  91. How to use a dynamic term id for the query block
  92. Querying Posts from ACF Repeater Subfield Select Value
  93. How to figure out correct wp_options to have autoload=’yes’
  94. Meta Query compare with LIKE pulls similar post types: 55 and 155, and 1,155
  95. How to show sticky posts on all pages of the pagination, not just the first page?
  96. How to get posts that have certain meta key value and order based on another meta key’s value
  97. Why does $_GET return values but get_query_var does not?
  98. Sort users in admin screen by meta value whether it exists or not
  99. Remove posts that start with similar words (like a delete duplicate posts plugin)
  100. Can I make a Heading block above a query loop block disappear when that query loop has no results?
Categories query Tags mysql, post-meta, query
Get multiple tags by slug
Plugins upload to wordpress in wampserver via filezilla

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