WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Updating meta value for 1,000s of posts (7 posts)

  1. Justin Tadlock
    Member
    Posted 4 years ago #

    I've got a situation where I need to update the meta value for tons of posts (example post 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_meta meta 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 ) );
  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    Error with this line.

    $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 ) );

    Missing double quote before UPDATE.

    I'm going to test your query anyway, just spotted that when copying so thought i'd give it a mention.

  3. Justin Tadlock
    Member
    Posted 4 years ago #

    Yeah, that's an error with my typing this without much sleep. It should have looked like this:

    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 ) );
  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    There's also a mistake here.

    "SELECT 'ID' FROM

    The quotes shouldn't be there. You'll get a return value of ID instead of the actual numeric IDs with the quotes there.

    Another typo?

  5. Justin Tadlock
    Member
    Posted 4 years ago #

    Nope, that wasn't a typo. Let me test that out.

  6. Justin Tadlock
    Member
    Posted 4 years ago #

    Perfect! You've been a big help. I would've at least spent another few hours before noticing that.

    Thank you.

    For anyone else that needs this in the future, the corrected code is:

    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 ) );
  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    Glad i could help... :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags