最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can you receive the most recent permalink or terms of the newly saved post?

programmeradmin1浏览0评论

When using the Gutenberg Block Editor the normal hooks for saving a post don't have the same behavior as with the classic editor.

  • pre_post_update
  • save_post
  • wp_insert_post

For example, if you hook into the save_post-hook the $permalink and the $categories will not return the new value, but instead the old value. Which is not the behavior that I expected from the classic editor.

add_action( 'save_post', 'custom_save_post', 10, 3 );

function custom_save_post( $post_id, $post, $update ) 
{
  $permalink = get_permalink( $post_id ); 
  $categories = get_the_terms( $post_id, 'category' );
}

How can I make it work so that I can retrieve the pre-updated permalink and the post-updated permalink?

When using the Gutenberg Block Editor the normal hooks for saving a post don't have the same behavior as with the classic editor.

  • pre_post_update
  • save_post
  • wp_insert_post

For example, if you hook into the save_post-hook the $permalink and the $categories will not return the new value, but instead the old value. Which is not the behavior that I expected from the classic editor.

add_action( 'save_post', 'custom_save_post', 10, 3 );

function custom_save_post( $post_id, $post, $update ) 
{
  $permalink = get_permalink( $post_id ); 
  $categories = get_the_terms( $post_id, 'category' );
}

How can I make it work so that I can retrieve the pre-updated permalink and the post-updated permalink?

Share Improve this question edited Feb 29, 2020 at 18:00 Mark asked Feb 28, 2020 at 17:14 MarkMark 1,0291 gold badge15 silver badges27 bronze badges 10
  • 1 I can only tell you that all my test text files contain the proper contents - the correct/updated permalink in permalink.txt and the correct/updated category objects dump in categories.txt and terms.txt. So I didn't need to proceed to the fourth step (editing the core taxonomy.php file). Are you using the latest WordPress stable release? Have you tested on other hosts? This might be something specific to your host/server.. – Sally CJ Commented Feb 28, 2020 at 18:13
  • @SallyCJ I really appreciate you taking the time to test. Thanks for your comments. It's weird because it happend on my local computer but also on two different remote hosts. But it helps a lot that you did not notice anything, perhaps I should try a different computer. – Mark Commented Feb 28, 2020 at 18:37
  • 1 Ok, but I should mention that I tested only with the block editor enabled (i.e. not tested on the classic editor) and if you haven't, try testing the Quick Edit link on the Posts -> All Posts page. – Sally CJ Commented Feb 28, 2020 at 19:00
  • I am also using the block editor. Great tip about the Quick Edit link. The problem does not occur there. Could you confirm one more thing. If you edit the post the "Update" button will become clickable again after some duration right? Because mine is stuck even on fresh host, fresh wordpress, default theme. When it's stuck the bug appears on the full post (not the quick edit). – Mark Commented Feb 28, 2020 at 21:32
  • 1 So I kinda did expect you'd ask that question, and the answer is, the button works just fine for me - no "stuck-ing". So try to inspect the network - on Chrome, Ctrl + Shift + I then go to the Network tab and just check the relevant requests. In fact, the console might be able to tell you what's going on with the button getting stuck.. – Sally CJ Commented Feb 28, 2020 at 21:42
 |  Show 5 more comments

1 Answer 1

Reset to default 3

When using the Gutenberg Block editor you need to use different hooks to get the expected behavior.

  • rest_insert_{$this->post_type} The pre-update hook.
  • rest_after_insert_{$this->post_type} The post-update hook.

credits: @SallyCJ

发布评论

评论列表(0)

  1. 暂无评论