2016-06-27 20 views
0

タイトルに記載されているように、(他のプラグインとの互換性のため)主にACFカスタムフィールドに基づいてpost_excerptとpost_thumbnailの値を自動的に保存/更新します。これを実現するためにトリングながら今、私が最初に次の関数であること、2つの問題が発生しました:Wordpressの抜粋を設定し、カスタムフィールドに基づいてサムネイルを投稿

仕事、しかし、時にはそれが私が持っていることを意味、私は保存するだけで二時間を(値を更新しているようだん
function test_FeaturedImageSetByACF() { 

    $current_screen   = get_current_screen(); // Current admin screen needed to identify the current cpt 
    $current_cpt_name  = $current_screen->post_type; // Current cpt name 
    $current_cpt_support = 'thumbnail'; // We want to check if the CPT supports this feature 

    $post_id    = get_the_ID(); // Current post ID 
    $post_image_field  = get_field('post_head_img'); // ACF field we want to sync 
    $post_image_id   = $post_image_field['id']; // ACF image filed ID 
    $post_image_url   = $post_image_field['url']; // ACF image filed URL 

    // If current cpt supports thumbnails/featured images 

    if (post_type_supports($current_cpt_name, $current_cpt_support)) { 

     if (($post_image_url) AND (($post_image_url) != (get_the_post_thumbnail()))) { 

      delete_post_thumbnail($post_id); 
      set_post_thumbnail($post_id, $post_image_id); 

     } 

    } 

} 

add_action('save_post', 'test_FeaturedImageSetByACF', 13, 2); 
add_action('publish_post', 'test_FeaturedImageSetByACF', 10, 2); 

2回保存する)。私は間違ったフックや間違った優先順位などを使用していることを理解していますが、どれがどれであるかわかりません。

私が持っている第2の問題は、抜粋の抜粋についても同様のことを達成したいのですか?今では関数は以前のものと全く同じように見えますが、更新する値がわかりません。誰かが私を正しい方向に向けることができますか?事前に

おかげ

答えて

0

を渡され、私は、ユーザーJohnヒューブナーが正しい方向に私を指摘したaswell ACFのサポートフォーラムに投稿することを決めました。

add_action('save_post', 'flex_CustomExcerptSetByACF', 50); 
function flex_CustomExcerptSetByACF() { 

    global $post; 

    $post_id  = ($post->ID); // Current post ID 
    $post_excerpt = get_field('post_excerpt', $post_id); // ACF field 

    if (($post_id) AND ($post_excerpt)) { 

     $post_array  = array(

      'ID'   => $post_id, 
      'post_excerpt' => $post_excerpt 

     ); 

     remove_action('save_post', 'flex_CustomExcerptSetByACF', 50); // Unhook this function so it doesn't loop infinitely 

     wp_update_post($post_array); 

     add_action('save_post', 'flex_CustomExcerptSetByACF', 50); // Re-hook this function 

    } 

} 

add_action('save_post', 'flex_FeaturedImageSetByACF', 50); 

function flex_FeaturedImageSetByACF() { 

    $current_screen   = get_current_screen(); // Current admin screen needed to identify the current cpt 
    $current_cpt_name  = $current_screen->post_type; // Current cpt name 
    $current_cpt_support = 'thumbnail'; // We want to check if the CPT supports this feature 

    global $post; 

    $post_id    = ($post->ID); // Current post ID 
    $post_image_field  = get_field('post_head_img', $post_id); // ACF field 

    if (($post_id) AND ($post_image_field)) { 

     $post_image_id   = $post_image_field['id']; // ACF image filed ID 
     $post_image_url   = $post_image_field['url']; // ACF image filed URL 

     // If current cpt supports thumbnails/featured images 

     if (post_type_supports($current_cpt_name, $current_cpt_support)) { 

      if (($post_image_url) AND (($post_image_url) != (get_the_post_thumbnail()))) { 

       update_post_meta($post_id, '_thumbnail_id', $post_image_id); 

      } 

     } 

    } 

} 
0

あなたはacf/save_postフックを使用することができます。これは、ACF具体的な質問ではなく、一般的な1のよりとなっているので、オブジェクトIDが

add_action('acf/save_post', 'handle_acf', 20); 
function handle_acf($object_id) 
{ 
// Do your stuff 
} 
+0

:(削除される可能性があります投稿したり、何かのイベントで)これは私が抜粋部分とカスタムポストサムネイル/おすすめ画像の両方を使用したコードがある一方、興味がある人のために、トピックはhttps://support.advancedcustomfields.com/forums/topic/set-wordpress-excerpt-and-post-thumbnail-based-on-custom-field/で見つけることができますそれは動作しますが、同じ方法で "save_post"フックで行います。イメージを変更し、ポストを保存して何も起こりません(古いフィーチャイメージです)。ページをリフレッシュまたは開くと何も起こりません。私は2回目を保存し、それは最終的に変更され、カスタムフィールドと投稿サムネイルは同じ画像を表示します。 – Cerere

関連する問題