admin管理员组

文章数量:1122832

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!

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!

Share Improve this question edited Jul 2, 2024 at 1:42 mmm 3,7933 gold badges16 silver badges22 bronze badges asked Jul 1, 2024 at 21:17 Ahmed CAhmed C 32 bronze badges 1
  • from where come the new data ? edit your question to add a example. – mmm Commented Jul 2, 2024 at 1:44
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: block editorHow to Programmatically Trigger the Update of a Post in WordPress