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

MySQL Query To Select Post By Postmeta

If I understand the question you are trying to show both post on single.php something like this should work as a second loop.

Put This INSIDE the first loop. Do this so you can get the current post ID

$parent_id=the_ID();

// Second Loop
$query = new WP_Query( array ( 'post_type' => 'TYPE1', 'meta_key' => $parent_id ) );

while ( $query->have_posts() ) : $query->the_post();
//normal loop stuff such as the_content();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

Related Posts:

  1. Triple meta_key on custom SELECT query
  2. Lack of composite indexes for meta tables
  3. Safe to delete blank postmeta?
  4. How to get all term meta for a taxonomy – getting term_meta for taxonomy
  5. delete unused postmeta
  6. Short of raw SQL, can I query for multiple attachment metadata that have a given array key?
  7. Get updated post meta on save_post action?
  8. How to get the total number of meta_values based on a custom post type?
  9. Insert post metadata for all posts in CPT at once if metadata no existent
  10. Set Condition echo function get_post_meta
  11. Get a row from a separate table by matching a posts meta_key to a tables ID column
  12. Compare string with post id in wpdb and do stuff when match is found
  13. Which is faster wpdb & get_row or wp_query & ge_post_meta?
  14. Read post meta values, only if posts are public
  15. How to test the outcome of a wpdb query?
  16. How to delete duplicate records in wp_postmeta database table?
  17. wpdb->get_var – count author posts, meta value
  18. How to query and update one colum in postmeta table?
  19. Display current ranking of post as a number in post title
  20. get specific values from WordPress meta_value
  21. Get data from custom table and update relative post_meta based on meta_key
  22. “BS_” rows in postmeta table
  23. why is my postmeta table is so heavy
  24. Get table parameter and save in meta value
  25. Display multiple meta_key/meta_values by single SQL query
  26. Exclude category from DB query
  27. Fetching array of postmeta with $wpdb and in_array conditional
  28. Which query method to use? (edit- wpdb syntax problems)
  29. Using mysql queries to delete custom post types based on meta_value
  30. Cache metadata for set of posts
  31. Export posts with postmeta without ID?
  32. How to get all the related ids without array?
  33. How do I retrieve the slug of the current page?
  34. Using wpdb to connect to a separate database
  35. How do you properly prepare a %LIKE% SQL statement?
  36. Most efficient way to get posts with postmeta
  37. Get posts by meta value
  38. Should I use wpdb prepare?
  39. Explanation of update_post_(meta/term)_cache
  40. How to extract data from a post meta serialized array?
  41. How to return number of found rows from SELECT query
  42. How to save an array with one metakey in postmeta?
  43. WordPress is stripping escape backslashes from JSON strings in post_meta
  44. How can I get the post ID from a WP_Query loop?
  45. Why are simple updates to wp_postmeta’s “_edit_lock” so slow?
  46. Check if Post Title exists, Insert post if doesn’t, Add Incremental # to Meta if does
  47. Return only Count from a wp_query request?
  48. Use REGEXP in WP_Query meta_query key
  49. How to fetch Data in WordPress using MySQLi or $wpdb
  50. How to update_post_meta value as array
  51. Adding meta tag without plugin
  52. What’s the point of get_post_meta’s $single param?
  53. What is the different between an attachment in wp_posts and an attachment in wp_postmeta?
  54. wpdb->insert multiple record at once
  55. How to edit a post meta data in a Gutenberg Block?
  56. Can i use php sql functions instead of $wpdb?
  57. Sanitizing integer input for update_post_meta
  58. post formats – how to switch meta boxes when changing format?
  59. Execute action after post is saved with all related post_meta records (data)
  60. How to define composite keys with dbDelta()
  61. Get a single post by a unique meta value
  62. if get_post_meta is empty do something
  63. How we get_post_meta without post id
  64. How get post id from meta value
  65. What is the code to get the download link for a product in WooCommerce?
  66. So much data in postmeta
  67. advanced custom fields update_field for field type: Taxonomy
  68. $wpdb and MySQL Create Trigger
  69. update_post_meta not saving when value is zero
  70. find a random blogid across my multisite network that has at least one post published
  71. Content hooks vs User hooks
  72. $wpdb->delete column values IN ARRAY()?
  73. Meta compare with date (stored as string) not working
  74. Trying to get custom post meta through Jetpack JSON API [closed]
  75. wpdb->prepare function remove single quote for %s in SQL statment
  76. How to update/insert custom field(post meta) data with wordpress REST API?
  77. What is the advantage of the wp_options design pattern?
  78. Restrict post edit/delete based on user ID and custom field
  79. Custom query to get post names beginning with a digit
  80. Multipart/formatted MySQL query problem
  81. get_post_meta returning empty string when data shows in the database
  82. publish_post action hook doesn’t give post_meta_data
  83. Remove WordPress.org Meta link
  84. Remove post meta keys
  85. Alternative to mysql_real_escape_string
  86. How to access the post meta of a post that has just been published?
  87. Why time functions show invalid time zone when using ‘c’ time format?
  88. Increment value (value = value+1) of $wpdb->update
  89. Why is get_post_meta returning an array when I specify it as single?
  90. How to update/delete array in post meta value?
  91. Adding an assisting editor box to Post page
  92. Inserting Post Meta From SQL
  93. WPDB Multiple site’s posts and get featured images
  94. Search multiple meta keys at once
  95. Migrating non-WordPress CMS to WordPress, lots of data to move — possible solutions?
  96. importing data from non-wordpress mysql db
  97. Should I sanitize custom post meta if it is going to be escaped later?
  98. Get terms of posts with category with sql
  99. Add post meta based on another post meta value before publish post
  100. Why does dbDelta() not catch MysqlErrors?
Categories post-meta Tags mysql, post-meta, wpdb
How does WordPress remember which editor is being used?
How Do I Figure Out What File(s) to Edit from Looking at the Source Code [closed]

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