I have the following function that when I have a new post, I get the ID to get the title of the post and then use it to send it as a notification (Using Firebase Cloud Messaging). But these only work for POST
.
function send_message_on_publish($ID, $post) {
include('inc/functions.php');
$title = 'New post!';
$msgtext = $post->post_title;
send_push($msgtext, $title);
}
add_action( 'publish_post', 'send_message_on_publish', 10, 2 );
But I need to use this same function for a CUSTOM POST TYPE (post_type=tribe_events
), how could I get the ID and title of a post_type.
You're using the publish_post
hook which is quite literally only when a post
's post status is changed to publish
.
To answer your question specifically, WordPress has a dynamic hook for {$new_status}_{$post->post_type}
. Simply add the action hook:
add_action( 'publish_tribe_events', 'send_message_on_publish', 10, 2 );
And it should fire for your tribe_events
post type as well. Custom Post Types still have an ID
and post_title
record that's accessible identically to standard post
s.
function send_message_on_publish($ID, $post) {
include('inc/functions.php');
$title = 'New post!';
$msgtext = $post->post_title;
send_push($msgtext, $title);
}
add_action( 'publish_post', 'send_message_on_publish', 10, 2 );
add_action( 'publish_tribe_events', 'send_message_on_publish', 10, 2 );
With all that said, you may want to consider using Post Status Transitions instead, because publish_{$post_type}
hooks will run when the post_type
is first published and when it's updated.