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