0

I'm working on a WordPress project where I need to programmatically trigger the update of a post in the backend. The goal is to have WordPress process and update custom fields as if I had clicked the "Update" button in the post editor, but without actually changing any of the post data.

I tried using the wp_update_post function with just the post ID, but it didn't work as expected. Here is what I've tried:

function trigger_post_update($post_id) {
    // Verify if the post exists
    if (!get_post($post_id)) {
        return;
    }

    // Trigger the update process by calling wp_update_post with the post ID only
    wp_update_post(array('ID' => $post_id));
}

// Example usage: trigger the update for post with ID 1
trigger_post_update(1);

However, this code doesn't seem to trigger the update process properly for custom fields.

Question:

How can I programmatically trigger the update process for a post in WordPress so that custom fields and other related hooks are processed correctly, without changing the post data? please note that this problem is related to cron

Any help would be greatly appreciated!

1
  • from where come the new data ? edit your question to add a example.
    – mmm
    Commented Jul 2 at 1:44

1 Answer 1

0

The function as written there will succeed silently, since you haven't actually passed in any data to change. You should be able to see what I mean by altering your function slightly:

function trigger_post_update( $post_id ) {
   
    return wp_update_post( array( 'ID' => $post_id ), true );

}

You don't need the get_post() to check if it exists, wp_update_post() will do that for you and return a WP_Error if it doesn't (without the second parameter it will just return 0). On success the function will return the ID you passed in.

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.