I am using plugins that fire action hooks before and after a post save.

When a post is saved the functions hooked into those actions perform some calculations and put the results into a custom field. I need to programmatically trigger a post "save/update" so that those calculations and custom field updates are run once again.

In a nut shell: I need a function that will simulate a click on the Update button for all posts of a certain post type.

For example:

function update_all_posts () {

    $args = array(
       'posts_per_page'   => -1,
       'post_type'       => 'my_custom_post_type'
    $posts = get_posts( $args );

    foreach ( $posts as $post ) {

I have tried using wp_update_post($post); and wp_insert_post($post); but none of the posts from the foreach loop are updated.

Please note; I don't have any specific data to pass in for the update. The values for the updated custom fields are coming from the post save calculation. If I absolutely must pass something in I suppose I could just pass in it's current values.

  • How is the change you want to perform hooked to begin with? save_post? – Jacob Peattie Jul 10 '18 at 6:18
  • @JacobPeattie A plugin I am using has added action hooks that fire before and after a post save, and I am hooking into those to perform the changes. This is the pre-save one and this is the post save one – Ben Turner Jul 10 '18 at 6:19
  • Show that code. What exactly are you calculating and based on what values? What hooks do you use to hook into? – Krzysiek Dróżdż Jul 10 '18 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.