You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用WP_Query获取CPT Program的testimonial元框数据用于ACF导入

迁移Program自定义文章类型的Testimonial数据到ACF Repeater

1. 确认元数据键名

从旧站点模板代码中,可直接提取出三个Testimonial对应的元数据键:

  • testimonials_first_testimonial
  • testimonials_second_testimonial
  • testimonials_third_testimonial

这些数据存储在WordPress的wp_postmeta表中,关联program类型的文章ID。

2. 导出旧站点数据

将以下代码添加到旧站点的主题模板(比如page-testimonial-export.php),或创建临时PHP文件上传到主题目录,访问对应页面即可获取所有Testimonial及所属文章信息:

// 查询所有已发布的Program文章
$args = array(
    'post_type' => 'program',
    'post_status' => 'publish',
    'posts_per_page' => -1, // 获取全部文章
);

$program_query = new WP_Query($args);

if ($program_query->have_posts()) {
    $all_testimonials = array();

    while ($program_query->have_posts()) {
        $program_query->the_post();
        
        // 获取当前文章基础信息
        $program_title = get_the_title();
        $program_url = get_permalink();
        
        // 读取三个Testimonial字段
        $testimonial_1 = get_post_meta(get_the_ID(), 'testimonials_first_testimonial', true);
        $testimonial_2 = get_post_meta(get_the_ID(), 'testimonials_second_testimonial', true);
        $testimonial_3 = get_post_meta(get_the_ID(), 'testimonials_third_testimonial', true);
        
        // 整理非空的Testimonial数据
        if (!empty($testimonial_1)) {
            $all_testimonials[] = array(
                'testimonial_text' => $testimonial_1,
                'program_title' => $program_title,
                'program_url' => $program_url
            );
        }
        if (!empty($testimonial_2)) {
            $all_testimonials[] = array(
                'testimonial_text' => $testimonial_2,
                'program_title' => $program_title,
                'program_url' => $program_url
            );
        }
        if (!empty($testimonial_3)) {
            $all_testimonials[] = array(
                'testimonial_text' => $testimonial_3,
                'program_title' => $program_title,
                'program_url' => $program_url
            );
        }
    }
    
    wp_reset_postdata();
    
    // 打印数据(可直接复制或调整为CSV导出)
    echo '<pre>';
    print_r($all_testimonials);
    echo '</pre>';

    // 可选:导出为CSV文件
    /*
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=program_testimonials.csv');
    $output = fopen('php://output', 'w');
    fputcsv($output, array('Testimonial内容', '所属Program标题', '所属Program链接'));
    foreach ($all_testimonials as $item) {
        fputcsv($output, array($item['testimonial_text'], $item['program_title'], $item['program_url']));
    }
    fclose($output);
    exit;
    */
} else {
    echo '未找到Program类型的文章';
}

3. 导入到新站点ACF Repeater

如果需要批量导入到新站点的ACF Repeater字段,可在新站点使用以下代码(替换字段键为你实际设置的名称):

// 替换为你新站点ACF Repeater的字段键
$repeater_field_key = 'program_testimonials';
// 替换为Repeater内子字段的键名
$text_subfield_key = 'testimonial_text';
$title_subfield_key = 'program_title';
$url_subfield_key = 'program_url';

// 查询所有Program文章
$args = array(
    'post_type' => 'program',
    'post_status' => 'publish',
    'posts_per_page' => -1,
);

$program_query = new WP_Query($args);

if ($program_query->have_posts()) {
    while ($program_query->have_posts()) {
        $program_query->the_post();
        $post_id = get_the_ID();
        
        // 读取元数据(如果是从旧站点迁移数据库,可直接读取;否则需调整为导入逻辑)
        $testimonial_1 = get_post_meta($post_id, 'testimonials_first_testimonial', true);
        $testimonial_2 = get_post_meta($post_id, 'testimonials_second_testimonial', true);
        $testimonial_3 = get_post_meta($post_id, 'testimonials_third_testimonial', true);
        
        $repeater_data = array();
        if (!empty($testimonial_1)) {
            $repeater_data[] = array(
                $text_subfield_key => $testimonial_1,
                $title_subfield_key => get_the_title($post_id),
                $url_subfield_key => get_permalink($post_id)
            );
        }
        if (!empty($testimonial_2)) {
            $repeater_data[] = array(
                $text_subfield_key => $testimonial_2,
                $title_subfield_key => get_the_title($post_id),
                $url_subfield_key => get_permalink($post_id)
            );
        }
        if (!empty($testimonial_3)) {
            $repeater_data[] = array(
                $text_subfield_key => $testimonial_3,
                $title_subfield_key => get_the_title($post_id),
                $url_subfield_key => get_permalink($post_id)
            );
        }
        
        // 更新ACF字段
        if (!empty($repeater_data)) {
            update_field($repeater_field_key, $repeater_data, $post_id);
        }
    }
    wp_reset_postdata();
    echo '数据批量更新完成';
}

注意事项

  • 操作前务必备份数据库,避免数据丢失
  • 替换代码中ACF字段键为你新站点实际创建的字段名称
  • 建议先在本地测试环境验证代码,再部署到正式站点

内容的提问来源于stack exchange,提问作者Trishah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 18:05:23