[英]How to get the tags on publish the post in all cases?
要求:在每個發布/更新帖子上,我們需要帖子中的所有標簽並對其執行一些功能。
案例 1:創建帖子,添加標簽並立即發布 === 完美工作 ===
案例 2:更新舊帖子並點擊更新按鈕。 === 完美運行 ===
案例 3:創建帖子,添加標簽並將其保存到草稿並關閉.. 再次打開同一個帖子,然后點擊發布而不更改任何內容 === 失敗 === 帖子結果中沒有任何內容.. 沒有找到標簽,但已經有標簽在帖子中可用,但如果我們更改某些內容或只是刪除並添加相同的標簽,則它可以工作。
我想要第三個案例工作..如何在發布時獲取草稿帖子的標簽..
下面是我的代碼
// This is hook which calls automatically on publish the post
add_action( 'publish_post', array($this, 'post_published_notification'), 10, 2 );
// The function calls on publish the post
public static function post_published_notification( $ID, $post )
{
$request_body = file_get_contents('php://input');
PostPublishNotification::saveDataInNotificationTable($ID,$request_body);
}
// We neen the tags here..
public static function saveDataInNotificationTable($ID,$request_body)
{
$post_data = json_decode($request_body);
// Here is nothing when I publish the drafted post. otherwise it works when I publish/update the new/old post
var_dump($post_data);
exit;
$tags = $post_data->tags;
if(isset($tags) && is_array($tags) && count($tags) > 0)
{
// SOME CODE
}
}
找到解決方案:
如果 request_body 中沒有數據,我們也可以從我們已有的 postID 中獲取它。
更改代碼:如果數據不可用,則添加條件,然后從帖子 ID 中獲取
public static function saveDataInNotificationTable($ID,$request_body)
{
$post_data = json_decode($request_body);
$tags = $post_data->tags;
if($post_data == "" || $post_data == null)
{
$tags = get_the_tags($ID);
}
if(isset($tags) && is_array($tags) && count($tags) > 0)
{
// SOME CODE
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.