[英]Get the id and title of custom post type for send notifications with FCM
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). 我具有以下功能 ,当我有新帖子时,我会获得ID以获取帖子的标题,然后将其用作通知发送(使用Firebase Cloud Messaging)。 But these only work for
POST
. 但是这些仅适用于
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. 但是我需要对CUSTOM POST TYPE(
post_type=tribe_events
)使用相同的函数,如何获得post_type的ID和标题。
You're using the publish_post
hook which is quite literally only when a post
's post status is changed to publish
. publish_post
,您正在使用publish_post
挂钩,仅当post
的帖子状态更改为publish
时才使用。
To answer your question specifically, WordPress has a dynamic hook for {$new_status}_{$post->post_type}
. 为了具体回答您的问题,WordPress对
{$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. 它也应该为您的
tribe_events
帖子类型tribe_events
。 Custom Post Types still have an ID
and post_title
record that's accessible identically to standard post
s. 自定义帖子类型仍然具有
ID
和post_title
记录,其访问方式与标准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 );
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. publish_{$post_type}
,您可能要考虑使用“ 发布状态转换” ,因为publish_{$post_type}
挂钩将在首次发布post_type
并对其进行更新时运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.