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
1 Answer
Reset to default 0The 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
版权声明:本文标题:block editor - How to Programmatically Trigger the Update of a Post in WordPress? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736301983a1931370.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论