I'm trying to set a meta property to a new post's permalink using update_post_meta. When I run this on wp_after_insert_post I get a permalink that ends in "?p=[ID]". If I update the post to run the function again the permalink is updated to something that ends in, "/[category]/[post-name-slug]/" which is what I want to insert originally.
How can I delay update_post_meta until the permalink is resolved? I was thinking of using add_action/do_action to delay when I call update_post_meta but I'm using the post ID to get the permalink in the function. Any recommendations?
Here's my current code:
add_action('wp_after_insert_post', 'add_permalink_to_new_post', 0, 4); function add_permalink_to_new_post( $post_id, $post, $update, $post_before ) { // Only run this on a newly created post if (!empty($post_before)) { return; } update_post_meta( $post_id, 'canonical_permalink', (string) get_permalink($post_id) ); }