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

A better way of getting draft posts that has a particular post meta using get_posts function

You can use the post meta arguments that get_posts function already supports. So, there is no need to use get_post_meta function in a PHP loop.

For your use case, post meta related arguments can be used in one of the two possible ways:

  1. Either using meta_key, meta_value, meta_compare arguments;

  2. Or, using the meta_query argument which supports an array of different post meta checks and hence is more powerful.

Using any of these methods returns all the necessary post data in one go.

Method-1:

The following example uses meta_key, meta_value, meta_compare arguments to achieve what you want:

function karma_get_pre_publish_posts() {
    $args = array(
        'post_type'    => 'post',
        'post_status'  => 'draft',
        'meta_key'     => 'approve',
        'meta_value'   => 'pre-publish',
        'meta_compare' => '='
    );

    return get_posts( $args );
}

Method-2:

The meta_query argument on the other hand requires an array of one or more argument arrays to query the posts with related post meta.

Like this:

function karma_get_pre_publish_posts() {
    // Yes, $meta_query argument has to be coded as an array of arrays.
    $meta_query = array(
        array(
            'key' => 'approve',
            'value' => 'pre-publish',
            'compare' => '='
        )
    );
    $args = array(
        'post_type' => 'post',
        'post_status' => 'draft',
        'meta_query' => $meta_query
    );

    return get_posts( $args );
}

Further reading:

  • Post meta related arguments (or parameters) of the get_posts function come from the parse_query method of WP_Query class. You may check the parse_query method documentation for more details.

  • Also, since the internal implementation related to post meta arguments is using the WP_Meta_Query class, you will also find some details in WP_Meta_Query class and WP_Meta_query constructor documentations.

There are user contributed examples in these documentations with more advanced use cases, including multiple post meta comparisons in a single query.

Related Posts:

  1. Ordering posts by metadata
  2. Post meta vs separate database tables
  3. How to return only certain fields using get_posts()
  4. Why Does get_posts() Return an Empty Set?
  5. Save metabox with multiple checkbox array
  6. get_posts / WP_Query Memory size of 134217728 bytes exhausted
  7. How to enable users to down-vote in this simple voting counter (that uses the post meta)?
  8. How can I free up the memory used by update_post_meta?
  9. Is there a way to list tags order by post_meta field
  10. Plugin development: get_post_meta is not working [closed]
  11. Act on user meta updated, but only once
  12. How to check if Woocommerce Order number equals to post ID?
  13. update post meta for checkbox in the admin when inserted in the front-end
  14. 403 Error when text is pasted in Custom Metabox Textarea
  15. get_posts() not working when accessing with a custom user role
  16. Saving value of a selection option in comment form as comment meta
  17. Add a Save Button to Custom Meta Box [duplicate]
  18. get post attachment using ajax
  19. Plugin with action ‘save_post’ needs to press publish twice on order to publish
  20. Doubts about the use of metadata and how this can affect performance on WordPress
  21. update_post_meta not working in a loop
  22. Run Shortcode of post’s custom field in functions.php / Plugin
  23. Get draggable widgets on Edit Post page
  24. How to get post content by get_posts function?
  25. Why does my widget break other widgets?
  26. What’s wrong with this post__not_in argument?
  27. Delist entries in the_loop
  28. WP nonce field checkbox prints checked=’checked’ outside input field
  29. Ajax: Populate with content from a post’s ID not working – duplicating current page html instead
  30. Issues Updating Post Meta with AJAX (Seems simple but cannot figure it out)
  31. Using delete_post_meta for deleting multiple selected options
  32. Get audio metadata on file upload
  33. Plugin can’t be activated [closed]
  34. Any way to update_post_meta with html content? It gets stripped and becomes empty
  35. Remove Meta-boxes (Yoast SEO plugin) [duplicate]
  36. Is there a way to add a link with add_post_meta?
  37. Adjust query on single
  38. Why is $_POST empty when saving custom Meta Box?
  39. Condition to check previous next article post title
  40. register_meta not showing custom post type metabox data in rest api
  41. Insert, update or remove data from database (usermeta)
  42. Unexpected issue when using attachment_fields_to_edit filter
  43. WordPress delete mysql rows with string
  44. Saving post meta
  45. Displaying Custom Post Meta
  46. Include add_post_meta in a plugin
  47. Custom query not working in plugin admin page
  48. How do I query posts and have their related taxonomies returned in the results?
  49. filter on get_posts efficiently
  50. Writing editor content to a file
  51. Using init hook for register_taxonomy is causing invalid_taxonomy in wp_insert_term()
  52. Plugin Form Submitting to admin-ajax.php instead of admin-post.php
  53. Unable to access custom plugin backend
  54. Plugin Development – Multilangual – Different pages
  55. Simple plugin don’t display content
  56. Screenshots on plugin page taken old [closed]
  57. How to translate wordpress backend to another language
  58. Plugin Development – Get Admin Url (Including cases where wp-admin is not used)
  59. How to create two custom post types when plugin is activated?
  60. How to hide page links from theme menu
  61. Get post thumbnail in WP_Query
  62. Brainstorm – Slow Query from Plugin Need to Speed It Up
  63. How can I save a password securely as a settings field
  64. Unexpected T_FUNCTION
  65. How to avoid conflicts with db.php / $wpdb and other plugins that decide to use them?
  66. Plugin settings are saving but the fields
  67. How to make a cronjob type plugin
  68. How to avoid conflict of plugin style and theme style?
  69. How do I debug an error that a plugin is causing?
  70. Adding an external stylesheet to a plugin
  71. Ajax +wordpress onClick link redirect to new page and create html content
  72. WP_NAV_MENU filter targets all menus
  73. Clean way to initialize plugin in newly-added site when plugin has been network activated?
  74. Get section of input passed to the sanitize_callback
  75. wp_ prefix changed. What is the way to change so any prefix is understood?
  76. If $var is empty, return 404.
  77. How to replace settings in WordPress plugin from a theme
  78. Remember the Meta Value With Radio Buttons
  79. Forward an old url rewrite scheme to a new one?
  80. Use options to control jQuery plugin
  81. Save temporary registration data
  82. Including template in shortcodes
  83. Activate / Deactivate plugin
  84. Is there a WordPress plugin to design WebGL? [closed]
  85. $wpdb in php 5.5
  86. how to display a string in admin_notices hook from a class plugin
  87. How to add a route?
  88. Create an user on external database
  89. Should I put my plugin javascript inline?
  90. Adding Dynamic Stylesheet
  91. Form submission to another page returning 404 error [duplicate]
  92. Resize not resizing images with Capitial extension like JPG
  93. Redirect returning users to a certain page?
  94. Is there any way to hide page from dashboard (all pages list) OR navbar from plugin function?
  95. Pagination not working with custom wp_query
  96. Get Posts by IDs (optionally)
  97. Passing values between enclosing and enclosed shortcodes
  98. How to get the number of pages when paginating comments?
  99. How does “Your comment is awaiting moderation” work?
  100. why does get_option( ‘home’ ) not work while using in plugin [closed]
Categories plugin-development Tags get-posts, plugin-development, post-meta
WordPress not remembering old slugs for pages
Redirect posts to post editor page based on query string

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