database - Update all published posts at once

admin2025-06-06  0

I know this question has been asked a few times but I can't find one with an adequate answer.

I need to run through all my posts and update them as in -> go to the edit screen a press Update - as some values for custom field values have been updated automatically.

Is there anyway of doing this automatically?

Running wp_update_post() etc. does not do this. Nor does Bulk Edit update.

Any thoughts would be most appreciated.

Thanks, D.

I know this question has been asked a few times but I can't find one with an adequate answer.

I need to run through all my posts and update them as in -> go to the edit screen a press Update - as some values for custom field values have been updated automatically.

Is there anyway of doing this automatically?

Running wp_update_post() etc. does not do this. Nor does Bulk Edit update.

Any thoughts would be most appreciated.

Thanks, D.

Share Improve this question edited Nov 1, 2018 at 13:07 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Nov 1, 2018 at 12:09 user142553user142553 212 silver badges9 bronze badges 5
  • What is this other database table? What is the code that is running when you update? Is it a plugin? – Jacob Peattie Commented Nov 1, 2018 at 12:18
  • Well it's not just the database table there are also other changes that need to happen automatically. Yes it's a plugin though. So I just need to understand really whether it's possible to run something that will emulate Edit -> Update for all posts – user142553 Commented Nov 1, 2018 at 12:26
  • Does anyone know whether Bulk Edit -> Update uses a different hook than Edit->Update? – user142553 Commented Nov 1, 2018 at 12:26
  • It depends on what hook the plugin uses to do its thing. You're going to need to ask the author for help if you're dealing with a 3rd-party plugin. Those are off-topic here. – Jacob Peattie Commented Nov 1, 2018 at 12:29
  • So it wasn't just the database table update it was some custom field change too. – user142553 Commented Nov 1, 2018 at 12:39
Add a comment  | 

1 Answer 1

Reset to default 1

I would have preferred to put this as a comment, but space does not allow for it so sorry about that. It might help you rather than being an answer. I am using 'product', but it could just as easy be 'post'.

function update_all()
{
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => -1,
    );
    $products_array = get_posts($args);
    if (!empty($products_array))
    {
        foreach ($products_array as $product)
        {
            echo "product : " . $product->ID;
            //Update whatever here, eg wp_update_post($product->id,$error);

        }
    }
    echo "</pre>";
}

As you have the product id for each product in a loop, you can get any meta or custom values for that product, and update them. I used this to bulk update prices for example. Obviously you would only want to run this from time to time, so I keep it remarked in the code. You could of course run it as a cron. I am a bit unclear on what you are trying to update. Looks to me like it's simply the publish date, which you could do in a loop.

I fail to see why wp_update_post in this loops wouldn't do it. Perhaps you need to enable displaying the errors when it runs.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749213472a317313.html

最新回复(0)