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

Assign / update custom field value for all posts (How can I assign only to posts without custom field value?)

You can use meta_query parameter (and Query all posts where a meta key does not exist) to limit the query to only posts that have or don’t have a certain meta value.

$query = new WP_Query(array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'your_meta_key',
            'compare' => 'NOT EXISTS',
        ),
    ),
    'no_found_rows' => true,
    'fields' => 'ids',
));

if ( $query->posts ) {
    array_walk(
        $query->posts,
        function($post_id, $index) {
            update_post_meta( $post_id, 'your_meta_key', 'meta_value' );
        }
    );
}

Also, depending on your hosting, you may want to set a limit to the posts_per_page and run the script multiple times (cronjob maybe?) instead of using -1 as the query for thousands of posts might crash the site or the execution time could run out.

Related Posts:

  1. get_post_meta shortcode returns empty field
  2. Update Multiple Post Meta for the Same Post In One call?
  3. Adding Custom Fields for Img in Posts
  4. how to display all posts Custom fields dynamically?
  5. How to get custom field image url of specific size
  6. Add multiple attributes to product from php
  7. Make the checkbox meta field checked by default
  8. Is Wrapping intval() Around esc_attr() Redundant for Escaping Input?
  9. Creating a custom post type, adding custom meta fields, preventing all future editability of posts of this type
  10. Redirect to another page using contact form 7? [closed]
  11. How to create repeater field manually, without plugin (ACF Pro)?
  12. Creating New Dynamic Fields for a Certificate (Number Generation, Code Referencing, and more)
  13. What snippet do I need to type to show my ACF field show up on my theme?
  14. Is there a way to make [Table Of Content] plugin while not using revision data?
  15. need to find duplicated meta value in custom filed and view the posts that have the same value
  16. WordPress hide post from custom post-type on a single page
  17. Custom Field used to allow a Free Story; no longer works
  18. Get and Update Most Meta Value as an array in HTML form
  19. How to get post that has non zero or greater than zero meta value
  20. Problem with conditional woocommerce custom checkout field
  21. Same Title on two different post type with single custom taxonomy
  22. Creating a user ‘add custom field’ section
  23. WordPress User Frontend Editing Custom Fields
  24. ACF: Hide a div or template section when a custom field (in a field group) is empty
  25. Fields are not displayed on front end under custom php code in Advanced Custom Fields and Flexible Content field WordPress
  26. Custom Dynamic Tag in Elementor not showing image
  27. Sanitizing, Validating and Escaping in WordPress (Plugin)
  28. Get total number of comment of the posts written by an author
  29. Plugin not saving values when placed in a folder within /wp-content/plugins
  30. Security checking in meta_box save is reluctant?
  31. How do you remove plugin edit option?
  32. Escape when echoed
  33. Scope for PHP Variables Assigned in functions.php or a plugin
  34. add_meta_box (Will display only in specific page admin WordPress)
  35. Make specific products accessible only to a user role in WooCommerce
  36. force customers to add only single item to card per purchase EDD [closed]
  37. Undefined property: WP_Post_Type::$ID
  38. Plugin exceeds memory limit
  39. How to get post URL in the_content filter?
  40. How to remove xmlns on language_attributes()?
  41. How to write one comment and publish on every post using database or plugin?
  42. how to increase custom post value by one most efficiently?
  43. Add a plugin before main container
  44. Shortcode to generate and save password in a file
  45. How to get subscription key or id using Woocommerce Subscriptions [closed]
  46. Last modified field for user profile?
  47. Create a random unique 6 digit number as custom field for custom post type
  48. How to get member list based on role by using buddypress?
  49. I don’t understand how this parameter works..?
  50. Why wp_mail() function isn’t sending any emails and displaying ‘0’ in Chrome ‘Network’ response
  51. How to initialize something in unit test before the init hook being called?
  52. Missing argument 3 for wp_register_sidebar_widget()
  53. Make id column as AUTO INCREMENT on plugin activation
  54. How to Include a Loop Template File in a Plugin
  55. Write to / remove from default .htaccess file from plugin?
  56. WordPress CMB2 – Run function on save
  57. FacetWP group listings by custom field [closed]
  58. Update Option Error: Notice: Undefined index
  59. How can I display a contact form for out of stock products in WooCommerce?
  60. Pass a php string to a javascript variable
  61. get shortcode value
  62. How to get number of Affected rows from wordpress dbDelta() function
  63. Show price with Geo IP location
  64. Where to hook my plugin’s action
  65. WordPress Ajax Callback
  66. Add category to body class
  67. The called constructor method for WP_Widget in GFWidget is deprecated since version 4.3.0! Use
  68. Plugin Development sqlite or WordPress’ database
  69. How to review WordPress website php version compatibility?
  70. printf – problem to understand code
  71. Custom Post Type Plugin not loading category template and loading 404 instead
  72. Sending email from inside a plugin using PHP PEAR
  73. Auto delete WordPress users according to time
  74. How to cancel WordPress’ action/filter when using OOP with anonymous callback
  75. Is there a filter for get_post_custom()?
  76. WordPress Plugin Page is Loading in Admin Content Container Instead of Separate Page
  77. How to trigger $_GET request within admin plugin page?
  78. Wp favorite posts get user favorites in profile using buddypress [closed]
  79. Storing Options in a Shortcode
  80. Why isn’t admin_notices displaying my text? [Simple Plugin, Beginner]
  81. How to generate video out of images via WordPress plugin
  82. how do i remove posts from a WP_Query so the pagination is right?
  83. Saving Custom Post types and fields to a custom table
  84. dynamically generating plugin syntax
  85. Custom Field Template seriously messed up in WP 3.1.1
  86. How can I get WordPress to save comments in markdown format?
  87. add_query_arg not working
  88. problems with wordpress and php version 5.3.3-1
  89. WooCommerce get_price returning wrong price when used via ajax
  90. How to ‘clone’ a wp plugin to make small changes
  91. Open Post Thumbnail in New Child Theme File in WordPress
  92. add mediaelement.js plugins into WordPress video player control bar
  93. Shortcode button dosent work for all posts. Work for first post only
  94. Where should I save an API key of my user when installing my plugin?
  95. Prevent a plugin from being automatically activated
  96. Programatically download a plugin
  97. Accessing post’s meta data based on user’s click of a post
  98. Error shown for Trying to get property ‘roles’ of non-object in WordPress After Content for User Roles
  99. Preferred way of cacheing a value in php
  100. Redirecting to home page after login as custom role WordPress
Categories plugins Tags custom-field, php, plugins, post-meta
How can I get the number of custom post type posts that have a specific attachment image set?
Verify if the current page has at least one published child

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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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