問題描述
publish_post Runs when a post is published, or if it is edited and its status is “published”. Action function arguments: post ID.
我已經將 publish_post 鈎子添加到我正在編寫的 WordPress 插件中。鈎子本身調用的函數是使用 wp_update_post 函數來更改多個帖子的類別。
這個鈎子不行,因為從運行結果返回的結果 wp_update_post 總是 0. 我最好的猜測是,運行 wp_update_post 導致另一個我的鈎子的一個實例,因為它的 re-publishes 的帖子… 我相信帶來了 「…」 或者如果被編輯,並且其狀態是上述聲明的”published”” 。
有沒有其他可以使用的 action-hook,只有當該帖子被添加為全新且未被編輯時才會被調用?
<?php
/*
Plugin Name: Category Switcher Plugin
Plugin URI: http://www.example.com
Description: When a new post is created this plugin will cause the
Version: 0.1
Author: Me
License: GPL2
?>
<?php
class categoryShifter {
function shiftCategories($post_ID) {
$maxNumPostsFirstTeir = 4;
$first_teir_cat = "Fresh News Stories 1";
$second_teir_cat = "Slightly Dated Stories 2";
$firephp = FirePHP::getInstance(true);
$firephp->info('BEGIN: categoryShifter.shiftCategories()');
$firephp->log($post_ID, 'post_ID: ');
$firephp->trace('trace to here');
$first_teir_id = categoryShifter::getIDForCategory($first_teir_cat, $firephp);
$second_teir_id = categoryShifter::getIDForCategory($second_teir_cat, $firephp);
$firephp->log($first_teir_id, '$first_teir_id');
$firephp->log($second_teir_id, '$second_teir_id');
$qPostArgs = array(
'numberposts' => 100,
'order' => 'DESC',
'orderby' => 'post_date',
'post_type' => 'post',
'post_status' => 'published',
'category_name' => $first_teir_cat
);
$firstTeirPosts = get_posts($qPostArgs);
$firephp->log($firstTeirPosts, 'got posts:');
$firephp->log(sizeof($firstTeirPosts), 'sizeof');
// NOTE: This appears to work.
for($i = sizeof($firstTeirPosts)-1; $i > $maxNumPostsFirstTeir-4; $i--)
{
$newCats = array($second_teir_id);
$editingId = $firstTeirPosts->ID;
$result = wp_set_post_categories($editingId, $newCats); /* NOTE: Doesn't work presently... returns an array with the $second_teir_id in it. */
$firephp->log($result, 'Result');
}
/*
$my_post = array();
$my_post['ID'] = 132;
$my_post['post_category'] = array($second_teir_id);
$firephp->log('Before', 'Before');
if(wp_update_post( $my_post ) == 0) {
$firephp->Error('Fatal Error, Post not updated', 'error');
}
$firephp->log('After', 'After');
*/
return $post_ID;
}
function getIDForCategory($cat_name, $logger) {
$logger->Info("Begin: getIDForCategory()");
$cats = get_categories();
$whichCatId = "";
foreach($cats as $single_cat) {
if($single_cat->name == $cat_name) {
$whichCatId = $single_cat->term_id;
break;
}
}
$logger->Info("End: getIDForCategory()");
return (int)$whichCatId;
}
}
/* Hook Post Creation */
/* add_action('publish_post', array('categoryShifter','shiftCategories')); */
add_action('wp_insert_post', array('categoryShifter', 'shiftCategories'));
?>
我現在已經切換到使用 wp_insert_post 掛鈎… 但是我仍然無法獲取 wp_set_post_categories 功能來更改帖子的類別。
我明白,我可能需要更新此代碼,以便它考慮到現有的職位類別,只修改由插件指定的類別,但現在它只是一個 alpha 。
最佳解決思路
add_action('new_to_publish', 'your_function');
add_action('draft_to_publish', 'your_function');
add_action('pending_to_publish', 'your_function');
次佳解決思路
確切地説,創建新職位的目標其實比看起來更棘手。技術上,有多種方式可以創建或更新,並且有很多不是很明顯的東西,技術上的帖子 (例如修改) 。
WordPress 提供了動態掛鈎,不僅跟蹤後創建,而且它是什麼和它成為什麼。見食典中的 Post Status Transitions 。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。