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
- __(): What if I have to pass in a variable?
- Run WP-CLI using PHP
- How can I make my custom shortcode work in a Custom HTML Widget?
- Update all posts automatically when using post_meta
- PHP Deprecated: Non-static method should not be called statically
- Post source link plugin – small modification
- Which hook callback has priority if both plugin and theme use the same hook?
- Ajax call doesn’t work in frontend but it’s working in backend (when I’m logged in)
- How can I add a custom meta value on file upload?
- How can update custom meta for all posts
- Check if a class exists within a method
- Datepicker not supporting timepicker
- Escape special characters in image link
- Get total number of comment of the posts written by an author
- Auto delete WordPress users according to time
- How to cancel WordPress’ action/filter when using OOP with anonymous callback
- Programatically download a plugin
- Error shown for Trying to get property ‘roles’ of non-object in WordPress After Content for User Roles
- Theme my Login plugin, how to update fields
- CSS from textarea in options page to frontend what to do
- wp_set_auth_cookie causes 403 error in the wooCommerce checkout
- Adding a new field to the address field type in gravity forms
- Show admin notice on incorrect value on form field
- WooCommerce specifc variations for specific user role [closed]
- Nested shortcode functions accessing variable
- Passing Page ID used in Meta Box Creation to JavaScript
- Setup wp_schedule_event within a custom plugin
- How to insert multiple postmeta values with one query?
- Does having more than 30 Admin Ajax affects site performance (plugin)?
- execute function after one completed
- WHy custom plugin slows down the loading of the pages?
- How to Resize the Custom Post Images?
- How to get specific string/value from an array? php [closed]
- Background Music WP [closed]
- Why is it important to check for isset and is_array before checking in_array in this example?
- How to delete a theme using AJAX
- How to call external functions from a PHP script in a WordPress plugin?
- Equivalent of admin.php for public pages
- Plugin that lets visitors Like a post (not facebook) and stores likes in custom meta?
- How to add custom function to pluggable.php
- WooCommerce – new order email hook
- How to list datas from database in a topic?
- Editing a coupon generating plugin
- WordPress fatal error from php protocol codes
- How use wp_insert_post and add og:custom tags?
- Display pagination in reservation Plugin and and Print table as pdf
- How to change “Read More” text?
- Adding Custom Endpoint in WordPress Rest API
- How to change a wordress plugin php code in my child’s functions.php file?
- What is the right way to populate a dropdown from MySql?
- make p tag collapsed after 3 rows
- General Term for this form Field
- Performance considerations – postmeta table versus new table for custom posts with foreign keys?
- WordPress Plugin PHP Not Calling Function
- load plugin in code snippet
- Generate and send ICS file through WordPress
- WP Plugin permissions – create new files
- Creating Admin Submenu Page via Class Method
- .htaccess file doesn’t work, with hundred tries
- ACF Repeater Field Question [closed]
- How to create a dashboard for logged-in users with custom functionality? [closed]
- Cannot access variables within a widget
- How do I convince this button to do something when it is clicked?
- Postal address auto-complete on profile page
- Doing action based on input from options menu
- WordPress Related Plugin – Adding an image
- Add meta tags with a plugin?
- How ACF Advanced Custom Field works with Woocommerce Single Product [closed]
- Strict Standards: Non-static method in sidebar generator
- Fast Tranfering my WordPress site to another server
- Duplicating wordpress install issue
- How to change all the urls of the WordPress site?
- I want to allow certain file types on dokan upload files
- How to create different woocommerce single_product.php pages for each product? [closed]
- Parse error : syntax error, unexpected ‘)’ in