使用rest api 创建文章时候自动更新文章主图和字段方法(使用n8n+deepseek自动翻译)

/**
 * Sync featured image and gallery meta when post is created/updated via REST API
 */
function sync_media_on_rest_insert($post, $request, $creating) {
    // Check if Polylang is active
    if (!function_exists('pll_get_post_translations')) {
        return;
    }
    
    // Get the post ID
    $post_id = $post->ID;
    
    // Check if this post has translations
    $translations = pll_get_post_translations($post_id);
    
    if (empty($translations) || count($translations) <= 1) {
        return; // No translations to sync
    }
    
    // Sync Featured Image
    $thumbnail_id = get_post_thumbnail_id($post_id);
    if ($thumbnail_id) {
        foreach ($translations as $lang_code => $translated_post_id) {
            if ($translated_post_id == $post_id) {
                continue;
            }
            set_post_thumbnail($translated_post_id, $thumbnail_id);
        }
    }
    
    // Sync Gallery Meta Field (adjust meta key as needed)
    $gallery_meta_keys = array('your_gallery_field_name', 'gallery_images'); // Add your meta keys
    
    foreach ($gallery_meta_keys as $meta_key) {
        $gallery_value = get_post_meta($post_id, $meta_key, true);
        
        if (!empty($gallery_value)) {
            foreach ($translations as $lang_code => $translated_post_id) {
                if ($translated_post_id == $post_id) {
                    continue;
                }
                update_post_meta($translated_post_id, $meta_key, $gallery_value);
            }
        }
    }
}

// Hook into REST API post insert/update for all post types
add_action('rest_after_insert_post', 'sync_media_on_rest_insert', 10, 3);
add_action('rest_after_insert_page', 'sync_media_on_rest_insert', 10, 3);

// For custom post types, add specific hooks
// add_action('rest_after_insert_YOUR_POST_TYPE', 'sync_media_on_rest_insert', 10, 3);
×
使用rest api 创建文章时候自动更新文章主图和字段方法(使用n8n+deepseek自动翻译)

使用rest api 创建文章时候自动更新文章主图和字段方法(使用n8n+deepseek自动翻译)

/** * Sync featured ...