2015-01-10 28 views
1

我遇到問題,我想使用另一個自定義帖子字段更新自定義用戶元數據字段。它必須在整個帖子後發生,包括自定義字段保存/更新。但是,我發現所有的電話:更新基本的發佈數據,然後調用操作,然後更新自定義字段。saved_post post_updated在更新自定義字段之前觸發,自定義字段更新後會觸發什麼功能? wordpress

saved_post & post_updated導致此代碼被延遲1保存。也就是說,如果我要創建一個新帖子並將$ my_value設置爲5,那麼我第一次保存它時會返回0,然後下一次它會返回5.等等。

有沒有人知道自定義發佈數據保存後運行的操作掛鉤?或者如何讓save_post或post_updated在自定義發佈數據之後觸發?

function zhu_li_do_the_thing($post_id) { 

//獲取數據後,筆者摘錄-ID和類型後

$post = get_post($post_id); 
$post_type = $post->post_type; 
$userid = $post->post_author; 

//如果你的我的自定義後的類型,然後獲取自定義字段的值,我想。

if('my_custom_post_type' == $post_type){ 
    $post_custom_fields = get_post_custom($post_id); 
    $my_custom_field = $custom_fields['im_a_field']; 
    foreach($im_a_field as $key){ 
     $my_value = $key; 
     } 

//如果該值以「+」或開始「 - 」做數學與它對抗的作者定製的元數據,如果是空忽略它,如果它是別的,使元=價值。

if(substr($my_value, 0,1)=='+' || substr($my_value, 0,1)=='-'){ 
     $my_int = intval($my_value); 
     $author_int = intval(get_user_meta($userid, 'the_objective, true)); 
     $result = ($author_int + $str); 
     update_user_meta($userid, 'the_objective', $result); 

    }elseif($my_value == null){ 
     return; 
    }else{ 
     update_user_meta($userid, 'the_objective', $my_value) 
    }}}; 

add_action('save_post, 'zhu_li_do_the_thing'); 

回答

0

您應該使用pre_post_update前檢查後元,並使用post_updated檢查更新的帖子元。

以下是檢查woocomerce產品庫存狀態的類的代碼片段。

function __construct() 
{ 
    add_action('post_updated', array($this, 'post_updated'), 1, 1); 
    add_action('pre_post_update', array($this, 'pre_post_update'), 1, 1); 
} 

function pre_post_update($post_ID) 
{  
    global $post_type; 

    if($post_type == 'product') 
     define('stock_status_previous', get_post_meta($post_ID, '_stock_status', 1)); 
} 

function post_updated($post_ID) 
{ 
    if(defined('stock_status_previous')) 
    { 
     $stock_status = get_post_meta($post_ID, '_stock_status', 1); 

     if($stock_status == stock_status_check) 
      return; 

     // post meta is different - sp do something 

}