我将 Gravity 与 ACF 一起用于帖子字段和插件 “高级帖子创建”,以便在用户填充表单时创建帖子。 在这些帖子中,我想填充一个 ACF 关系字段。
我的要求是: 我想知道如何使用重力复选框将ACF 关系字段填充到列出许多帖子标题的表单中?
这是我当前的代码和一些注释。 我正在使用 Gravity hook 和 ACF function 来更新相关关系字段。
// 6 is the form id
add_action( 'gform_advancedpostcreation_post_after_creation_6', 'apc_serialize_checkboxes', 10, 4 );
function gform_relation( $post_id, $feed, $entry, $form ) {
// Checkboxes field id.
$field_id = 21;
// Get field object.
$field = GFAPI::get_field( $form, $field_id );
if ( $field->type == 'checkbox' ) {
$checked = $field->get_value_export( $entry );
// The form entries look like this : Post title 1, Post title 2...
// As I only get "Post title" as values, I convert the "Post title" into ID
//in order to build an array for my ACF relation field.
foreach ($checked as $check) {
$my_post = get_page_by_title( $check, OBJECT, 'post' );
echo $my_post->ID;
}
// Convert to array
$values = explode( ', ', $checked );
}
// 'parcours_thematiques_choisies' is the ACF relation field key.
// The return format is "Post Id".
update_post_meta( $post_id, 'parcours_thematiques_choisies', $values );
}
感谢您的回馈!
晚上好,Fxmly92,正如您上面提到的,要在复选框字段中使用 GRAVITY 和 ACF,需要使用代码来转换数据,因为 GRAVITY 以一种方式存储数据,而 ACF 以另一种方式存储数据。
要仅使用一个复选框字段,请在主题的functions.php中使用以下代码:
// CONVERT CHECKBOX FROM GRAVITY TO ACF
add_action( 'gform_advancedpostcreation_post_after_creation_1', 'apc_serialize_checkboxes', 10, 4 );
function apc_serialize_checkboxes( $post_id, $feed, $entry, $form ) {
// Checkboxes field id.
$field_id = YOUR GRAVITY FIELD ID;
// Get field object.
$field = GFAPI::get_field( $form, $field_id );
if ( $field->type == 'checkbox' ) {
// Get a comma separated list of checkboxes checked
$checked = $field->get_value_export( $entry );
// Convert to array.
$values = explode( ', ', $checked );
}
// Replace my_custom_field_key with your custom field meta key.
update_post_meta( $post_id, 'YOUR ACF META KEY', $values );
}
将“您的重力场 ID”字段替换为表单中复选框的 ID,例如 4。
将 YOUR ACF META KEY 替换为 ACF 元密钥,例如 cars_brand。
如果您有多个复选框,您可以使用以下结构:
add_action( 'gform_advancedpostcreation_post_after_creation_1', 'apc_serialize_checkboxes', 10, 4 );
function apc_serialize_checkboxes( $post_id, $feed, $entry, $form ) {
// Checkboxes field ids.
$field_id_1 = YOUR GRAVITY FIELD ID;
$field_id_2 = YOUR GRAVITY FIELD ID;
// Get field objects.
$field_1 = GFAPI::get_field( $form, $field_id_1 );
$field_2 = GFAPI::get_field( $form, $field_id_2 );
$values_1 = array();
$values_2 = array();
// Check if the first field is a checkbox.
if ( $field_1->type == 'checkbox' ) {
$checked_1 = $field_1->get_value_export( $entry );
$values_1 = explode( ', ', $checked_1 );
}
// Check if the second field is a checkbox.
if ( $field_2->type == 'checkbox' ) {
$checked_2 = $field_2->get_value_export( $entry );
$values_2 = explode( ', ', $checked_2 );
}
// Remove any duplicates and empty values.
$values_1 = array_filter( array_unique( $values_1 ) );
$values_2 = array_filter( array_unique( $values_2 ) );
// Replace 'setores_tecnologicos' and 'outra_meta_key' with your actual meta keys.
update_post_meta( $post_id, 'YOUR ACF META KEY', $values_1 );
update_post_meta( $post_id, 'YOUR ACF META KEY', $values_2 );
}
如果需要添加其他选项,只需遵循结构即可。
回答此类问题的一个技巧是使用OPEN AI的CHAT GPT,你可以使用免费的CHAT GPT 3.5,它足以回答你。
我为我的英语道歉,我是巴西人,我对你们的语言不流利,但我希望我能帮助解决您的问题,我可以回答可能出现的任何新问题。祝您的项目顺利!