Reputation: 1424
I need to do some actions only on newly published posts that requires post metadata.
I've tried many different hooks, but they all also trigger for other "events" like updating post, or if they trigger only on publish, metadata is empty or just has _edit_lock
value inside.
auto-draft_to_publish
hook triggers when I need it, but there is no post meta
add_action(
'auto-draft_to_publish',
'wpse120996_specific_post_status_transition'
);
function wpse120996_specific_post_status_transition($post) {
if ($post->post_type != 'poruke') {
return;
}
$post_meta = get_post_meta($post->ID);
echo "<pre>";
die(var_dump( $get_post_meta ));
echo "</pre>";
}
transition_post_status
works with the right status checks, but there is no post meta as well'publish' === $new_status && 'publish' !== $old_status
// right time,no post meta
publish_post
it seems that this one doesn't even trigger for some reasonadd_action( 'publish_post', 'myfunction' );
function myfunction($post) {
echo "<pre>";
die(var_dump( 'PUBLISHED?' ));
// this dump is nowhere to be found,
// I looked in network tab in debugger
echo "</pre>";
}
save_post
this one triggers as soon as "Add New" is pressed in a sidebarI'm having a lot of problem with this and I have hard time believing that something so "basic" would not be a feature in WordPress, but I didn't find anything helpful in my search so far.
Thanks in advance.
Upvotes: 2
Views: 2988
Reputation: 1424
I ended up using the publish_post hook, originally It wasnt working because if you have custom post type it has to be used like this:
publish_yourCustomPostName
It still didn't have access to post meta from database because it's triggered before postmeta is saved to the database, but luckily I could access post meta from $_POST variable like this:
$_POST['acf']
Upvotes: 2
Reputation: 459
First, see my comment below your opening post.
Second, in the 1. example you post, you are dumping $get_post_meta
, but you meant to dump $post_meta
.
I noticed $post
has the post id number in it, not $post->ID
, so I sent $post
to get_post_meta
.
This seemed to work for me:
add_action( 'publish_post', 'myfunction' );
function myfunction($post) {
print_r(get_post_meta($post)); die();
}
Be sure to test it while making a new post of type post, not other custom post types or pages, like the link in the comment explained ( https://adambrown.info/p/wp_hooks/hook/publish_post ) , you need to change the hook to something else to use it on other post types. So 'publish_events' for post type events
.
UPDATE
My apologies, i see the resulting post_meta
has little info in it:
Array ( [_edit_last] => Array ( [0] => 1 ) [_encloseme] => Array ( [0] => 1 ) )
Maybe it is time to do a feature request to Wordpress (for a post publish hook).
Upvotes: 1