I have a simple function that automatically formats the whole post after publishing/updating.

It's hooked on:

add_action('save_post', 'myFunction');

I would like to format every post, that I already have in database. I know I can open every post and click on update to call the "save_post" hook on myFunction on the post, but my post database is pretty big..

Is it possible to run the function just once on every already published post somehow or update every post so the function will be called by the hook automatically?

2 Answers 2


Yes, this is possible. Something like this should affect all posts that are currently published. Literally run this once and then disable it by commenting out the add_action or you will have a bunch of unwanted queries to the database.

Running once: With this code placed in your functions.php, the function will run when the front end of your site is loaded in a browser. Load the home page of your site in a browser then disable the function. Confirm that your post format was applied to existing posts.

     add_action( 'init', 'my_function', 10);

     function my_function() {
        $args = array( 'post_type' => 'post', 'post_status' => 'publish');
        $pages = get_pages($args);
        foreach ( $pages as $page ) {
            // Do something here. Call your formatting function

Edit: If it absolutely must run not more than once, have your function look for an SQL table with a boolean value of true or false. If false or table does not exist, run the function, create the table and set it to true.

  • 1
    It's better to make a plugin and register the activation hook (register_activation_hook), so it'll run only on plugin activation. And here's a function to run only once without using the plugin or theme activation technique.
    – brasofilo
    May 30, 2013 at 18:22
  • I agree that's a good way to ensure it runs once.
    – iyrin
    Mar 14, 2018 at 7:41

Might also consider backing up database and trying an SQL find and replace. Depends on what you are trying to do to all of your posts:

UPDATE wp_posts SET post_content = replace(post_content, 'Find This String', 'Replace With This String');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.