I’ve got a situation where I need to update the meta value for tons of posts (
examplepost type), so I assumed the best way to do this would be to change it outside of the meta functions as those would have to be done individually with a foreach loop.
So, I’ve been brushing up on the wpdb class tonight and interacting with it.
What’s happening in the code is I need to get all the children of a deleted post parent (
$parent_id). Then, I need to update a specific meta value for the
example_metameta key for all of those posts.
This is what I have so far, but it doesn’t seem to be working. If anyone could look over the code to see if I’ve done anything wrong or if there’s a better option, I’d be very thankful.
global $wpdb; $query = $wpdb->prepare( "SELECT 'ID' FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'example'", $parent_id ); $children_ids = $wpdb->get_col( $query ); if ( count( $children_ids ) ) $wpdb->query( $wpdb->prepare( UPDATE $wpdb->postmeta SET meta_value = %d WHERE meta_key = 'example_meta' AND post_id IN( " . implode( ',', $children_ids ) . " )", $example_integer ) );
- The topic ‘Updating meta value for 1,000s of posts’ is closed to new replies.