$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>database - Update all published posts at once|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

database - Update all published posts at once

matteradmin8PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far