How to get the current permalink in wp_insert_post hook?

Question

I have two terms in the category-taxonomy: Fashion and Travel. In a clean WordPress environment without plugins and the most minimal custom theme. When a post is in Fashion and I switch to Travel and save. The permalink shows the old format with Fashion instead of Travel. When I save again, the correct permalink is shown.

Digging deeper into get_permalink() I found out that get_terms(array('taxonomy'=>'category','object_ids'=>$post_id)) is giving me Fashion back, instead of Travel. Unless I save again.

add_action( 'wp_insert_post', 'cm_wp_insert_post', 10, 3 );
function cm_wp_insert_post( $post_id, $post, $update )
{
    file_put_contents( dirname(__FILE__).'/log.txt', get_permalink( $post_id ) );
    // The log shows the old permalink. Only when saved again it shows the correct permalink.
}

Edit
The problem seems to be locally and not remotely. So that means out of the box it should work, which is probably why nobody has answered yet. I completely removed everything. Created a new host in MAMP Pro v5.7, with PHP 7.4.2. and Apache. Got the latest WordPress 5.3.2. and a clean MySQL database. Still the problem has not yet been resolved.

0
, , Mark 8 months 0 Answers 52 views 0

Leave an answer