2017-01-22 125 views
0

Wordpress + Timber + ACF Pro。在functions.php裏面,我有一個行爲,每當發佈一篇文章(有一週的類型)時就會觸發它。ACF自定義字段。在functions.php內獲取欄內動作

我想從這篇文章中獲取數據,並使用它爲每個用戶創建一個新帖子。

我有它追求時尚。當一篇文章發表後,我會抓取標題和用戶名,並將其用作新創建文章的標題。

但是,在嘗試提取ACF數據時遇到問題 - 例如:week_commencing date字段。所有ACF數據都返回NULL(我知道這些字段已填充)。

我已閱讀文檔 - 訪問數據的哪個狀態,您需要調用get_field('field_name','post_id') - 我已經完成了。

我寫出了$ ID - 所以知道這是正確的。

難道這是由於我運行的東西的順序嗎?

這裏是我的代碼:

function weekly_published_post_setup($ID, $post) { 
    $customers = get_users(); 
    $theDate = get_field("week_commencing", $ID); 

// Array of WP_User objects. 
foreach ($customers as $user) { 

     $new_post = array(
       'post_type' => 'weekly_tasks', 
       'post_title' => $post->post_title . ' - ' . $theDate . ' - ' . $user->display_name, 
       'post_content' => $theDate, 
       'post_status' => 'publish', 
       'post_author' => $user->ID 
     ); 
    wp_insert_post($new_post); 

     } 
} 
add_action('publish_week', 'weekly_published_post_setup', 10, 2); 

**編輯**

事實證明,正在創建的WordPress郵寄的ACF領域之前被保存?所以一個朋友重構我的代碼來使用不同的事件。然而,當帖子發佈時,這不會被觸發...

function week_published_delivery_setup($ID) { 

    $post = get_post($ID); 

    if ($post->post_type != 'week') { 
     return; 
    } 

    if($post->post_modified_gmt != $post->post_date_gmt){ 
     return; 
    } 

    $customers = get_users(); 

    $field = get_field('week_commencing', $ID); 

    $fields = post.get_field_objects($ID); 

    if($fields) 
    { 
     foreach($fields as $field_name => $field) 
     { 

       $tmp .= $field['label'] . $field['value']; 
     } 
    }*/ 



// Array of WP_User objects. 
foreach ($customers as $user) { 
     $new_delivery_post = array(
       'post_type' => 'delivery', 
       'post_title' => $post->post_title . ' - ' . $field . ' - ' . $user->display_name, 
       'post_content' => $post->post_title, 
       'post_status' => 'publish', 
       'post_author' => $user->ID 
     ); 
    wp_insert_post($new_delivery_post); 


     } 
} 
add_action('acf/save_post', 'week_published_delivery_setup', 20); 

回答

0

因此,現在測試發佈時的發佈狀態 - 然後執行所需的任務。觸發動作,如果old_status == future和new_status == publish似乎有訣竅。

function on_all_status_transitions($new_status, $old_status, $post) { 
    $ID = $post->ID; 
    if ($post->post_type != 'week') { 
     return; 
    } 

    if ($new_status != $old_status && $old_status == 'future' && $new_status == 'publish') { 

      $customers = get_users(); 

      $field = get_field('week_commencing', $ID); 



      // Array of WP_User objects. 
      foreach ($customers as $user) { 
       $new_delivery_post = array(
         'post_type' => 'delivery', 
         'post_title' => $post->post_title . ' - ' . $field . ' - ' . $user->display_name, 
         'post_content' => $post->post_title, 
         'post_status' => 'publish', 
         'post_author' => $user->ID 
       ); 
       wp_insert_post($new_delivery_post); 


       } 

    } 
} 
add_action( 'transition_post_status', 'on_all_status_transitions', 20, 3); 
相關問題