post meta – get_post_meta() returns nothing in save_post, publish_post, wp_after_insert_post
Question
I am trying to change the title of a custom post type “property”, i tried many ways.
- publish_property (ref: {$new_status}_{$post->post_type} Link)
- save_post (ref: save_post Link)
- wp_after_insert_post (ref: Link)
current code
add_action( 'publish_property', 'modify_property_url', 120, 3);
function modify_property_url( $post_id, $post, $old_status )
{
if ( $post_parent_id = wp_get_post_parent_id( $post->ID ) ) {
$post_id = $post_parent_id;
}
$addressArr = get_post_meta( $post_id, 'fave_property_address', true ); //doesn't work, god knows why
$address = $addressArr;
$addressCommasRemoved = (explode(",",$address));
$addressCommasRemoved = join("", $addressCommasRemoved);
$addressSpacesRemoved = (explode(" ",$addressCommasRemoved));
$newUrl = join("-", $addressSpacesRemoved);
$post_update = array(
'ID' => $post->ID,
'post_title' => $newUrl
);
wp_update_post( $post_update );
}
Common Mistakes tried to avoid:
- “property” is the post type.
- tried changing the priority upto 700 doesn’t works
P.S: the same code ( get_post_meta() ) when ran outside these hooks and actions works!
0
5 months
2022-09-21T20:00:55-05:00
2022-09-21T20:00:55-05:00 0 Answers
0 views
0
Leave an answer