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 all posts with certain meta data

I think you can do a left join :

left join 
    $wpdb->postmeta my_field_meta on (p.ID = my_field_meta.post_id and 
                                      my_field_meta.meta_key = 'subscribe') 

Where “subscribe” it’s the name of your custom field. So your code could be:

 $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(p.post_date)
    FROM $wpdb->posts as p
    LEFT JOIN 
        $wpdb->postmeta my_field_meta on (p.ID = my_field_meta.post_id and 
                                          my_field_meta.meta_key = 'subscribe')     
    WHERE MONTH(p.post_date) = '$thismonth'
    AND YEAR(p.post_date) = '$thisyear'
    AND p.post_type="post" AND p.post_status="publish"
    AND p.post_date < '" . current_time('mysql') . '\'', ARRAY_N);

Related Posts:

  1. what is the correct way to compare dates in a WP query_posts meta_query
  2. Order by meta value or date?
  3. Filtering posts by post meta data
  4. Custom query with query_posts doesn’t show post without certain meta_key
  5. How to sort by meta value?
  6. get_post_meta causes database queries
  7. Author Page Custom Query WHERE author OR [post meta value] OR [post meta value]
  8. Querying posts with meta value that begins with a certain pattern
  9. using multiple meta_key and meta_value in query_posts
  10. How can I sort homepage by a meta value?
  11. IF.. post meta show… Conditional Tag Help…?
  12. order posts by meta value on posts page
  13. Query_post($args)
  14. query_posts() on key’s value, or key’s existence
  15. meta_compare seems to be treating values as strings instead of integers as expected
  16. Fetch Record based on meta key dates
  17. how to make members list directory through wordpress post custom meta key.
  18. Custom fields (wp_post_meta) vs Custom Table for large amount of data
  19. How to retrieve an array of post IDs by a particular value stored in a custom meta’s array
  20. Query posts by meta_key whose value is an array
  21. Unable to get specific value from post meta
  22. Exclude posts by post meta value
  23. Frontend form with multiple posts
  24. query_posts orderby postmeta [closed]
  25. Get author total post votes from post meta
  26. How do i create a custom post query when the meta value is an array?
  27. Ordering Posts By Meta Data
  28. Limiting the amount of posts this displays
  29. How to Get All Posts with any post status?
  30. posts_per_page no limit
  31. Custom query with orderby meta_value of custom field
  32. Can I exclude a post by meta key using pre_get_posts function?
  33. Advanced search form with filters for custom taxonomies and custom fields
  34. How to update custom fields using the wp_insert_post() function?
  35. Can wp_query return posts meta in a single request?
  36. How can i get count from query post
  37. query_post by title?
  38. Why query_posts() isn’t marked as deprecated?
  39. Alternative to query_posts for main loop? [duplicate]
  40. how to query posts by category and tag?
  41. Custom post meta field effect on the performance on the post
  42. How to get custom post meta using REST API
  43. Difference between meta keys with _ and without _ [duplicate]
  44. Using WP_Query to Query Multiple Categories with Limited Posts Per Category?
  45. Orderby meta_value only returns posts that have existing meta_key
  46. What is the index [0] for on post meta fields?
  47. Using meta_query, how can i filter by a custom field and order by another one?
  48. How do I query by post format in WordPress 3.1
  49. What is “meta_input” parameter in wp_insert_post() used for?
  50. Display posts of the last 7 days
  51. Query Posts or Get Posts by custom fields, possible?
  52. Is there a way to exclude the content from the post variable to save on RAM usage?
  53. How to enable revisions for post meta data?
  54. Return all custom meta data for one custom post type
  55. The “_encloseme” Meta-Key Conundrum
  56. Best way to programmatically remove a category/term from a post
  57. How to query_posts using meta_query to orderby meta_key AND have a secondary sort by date?
  58. Query posts by custom taxonomy ID
  59. Display/query post formats
  60. Using get_post_meta with new_to_publish
  61. How to filter custom posts by tags and custom fields?
  62. Custom field values deleted when trashing custom post type
  63. How to get all custom fields of any post type
  64. How to query post by user role?
  65. Admin Area Custom Type Search By Meta Fields Without Title & Content
  66. How to return results of a get_posts() in explicitly defined order
  67. Custom field metabox not showing in back-end
  68. How do I Paginate Search Results for Custom Post Types?
  69. Help to condense/optimize some working code
  70. So much data in postmeta
  71. Can I count the number of users matching a value in a multiple value key?
  72. How to set posts per page using WP_Query()
  73. query_posts exclude a meta key
  74. How do I create a random post that will last for a day
  75. Ensuring sticky posts are retrieved first (without using two queries)?
  76. Getting attachments by meta value
  77. When using add_post_meta and update_post_meta, is there any way to give the individual arrays keys?
  78. Determine if more posts are available than was asked for in `query_posts()`?
  79. How to hide meta box values from custom fields list?
  80. Auto sort the wp-admin post list by a meta key
  81. Modify main WordPress loop with a parse_query filter
  82. query_posts() in function makes global $wp_query out of sync?
  83. get_post_meta() unserialize issue – returns boolean(false)
  84. What is the advantage of the wp_options design pattern?
  85. Limiting query_posts to 1, regardless of sticky post?
  86. How to make “sticky” pages (and query by them)
  87. meta_query where value is equal to given value
  88. Storing meta fields multiple times OR once with multi dimensional array?
  89. Query posts: how to exclude results if post is in multiple categories
  90. Update all posts automatically when using post_meta
  91. Allow user to create instances of custom field
  92. Query Custom Meta Value with Increment
  93. query_posts ->using meta_compare / where meta value is smaller or greater or equals
  94. Problem with ‘post__not_in’
  95. query_posts and only show results if a custom field is not empty
  96. How to order posts by descending comment count on taxonomy page?
  97. Display Custom Post Type Fields
  98. display specific custom fields
  99. Filter archive.php by custom meta
  100. Save metabox with multiple checkbox array
Categories query-posts Tags custom-field, post-meta, query-posts
WordPress category gives 404 after moving to a different domain
How to display a 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