2014-04-20 34 views
1

下面是我的代碼,它將採用post_id並從相同類別中隨機獲取3個帖子並將它們存儲爲自定義字段。代碼很明顯,因爲當我點擊「New Post」時,我看到自定義字段被填充,但是當我點擊「發佈」或「保存」時,下面的代碼沒有得到執行,根據我的理解,Save_Post在創建新帖子時會被調用一次,當您實際保存帖子時會再次被調用。Wordpress save_post動作保存時不會觸發

有趣的是,下面的代碼在本地服務器,WAMP中工作,但不在我的生產服務器上,我不知道爲什麼。他們都使用相同的插件。

function update_postmeta($post_id) { 
global $post; 

if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { 
return; 
} 

unset($rand_id); 

$cat_id = get_the_category($post_id); 

$args = array(
    'showposts' => 3, 
    'orderby' => 'rand', 
'cat' => $cat_id[0]->cat_ID, 
); 
$my_query = new WP_Query($args); 
while ($my_query->have_posts()) : $my_query->the_post(); 
    $rand_id = $rand_id.get_the_ID().','; 
endwhile; update_post_meta($post_id, 'related_id',$rand_id); 
} add_action('save_post', 'update_postmeta'); 

回答

1

嘗試

if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { 
return($post_id); 
} 
+0

謝謝你,這是不是解決辦法。無論如何,我寫了一個小程序爲我做上述。再次感謝你。 –

相關問題