这就是我最终设计的解决方案。如果任何人有任何改进,我们将不胜感激:
function add_acf_fields_to_postmeta_field($post_id, $post) {
if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return;
}
$post_faqs = get_field(\'questions_answers\', $post_id);
$post_faq_content = \'\';
if((\'post\' == get_post_type($post_id) || \'press\' == get_post_type($post_id)) && !$post_faqs) {
return;
}
if(\'faq\' == get_post_type($post_id)) {
// when a faq gets saved
// we find every posts/press that uses this faq
// then loop through and re-save all that post/press\'s faq contents to the meta field
$all_posts = new \\WP_Query(array(
\'post_type\' => array(\'post\', \'press\'),
\'meta_query\' => array(array(
\'key\' => \'questions_answers\',
\'value\' => $post_id,
\'compare\' => \'LIKE\'
)),
\'fields\' => \'ids\'
));
if($all_posts->have_posts()) {
foreach($all_posts->posts as $a) {
$post_faqs = get_field(\'questions_answers\', $a);
foreach($post_faqs as $p) {
$post_faq_content .= strip_tags(get_the_title($p).\' \'.get_field(\'answer\', $p));
}
update_post_meta($a, \'post_faq_content\', $post_faq_content);
}
}
} elseif((\'post\' == get_post_type($post_id) || \'press\' == get_post_type($post_id)) && $post_faqs) {
// when a post/press gets saved and it has faqs
// loop through them all and save contents to meta field
foreach($post_faqs as $p) {
$post_faq_content .= strip_tags(get_the_title($p).\' \'.get_field(\'answer\', $p));
}
update_post_meta($post_id, \'post_faq_content\', $post_faq_content);
}
}
add_action(\'save_post\', __NAMESPACE__.\'\\\\add_acf_fields_to_postmeta_field\', 10, 2);
add_action(\'publish_post\', __NAMESPACE__.\'\\\\add_acf_fields_to_postmeta_field\', 10, 2);
add_action(\'publish_press\', __NAMESPACE__.\'\\\\add_acf_fields_to_postmeta_field\', 10, 2);
add_action(\'publish_faq\', __NAMESPACE__.\'\\\\add_acf_fields_to_postmeta_field\', 10, 2);