Wordpress:如何使用 ACF 和自定义 post 类型制作唯一字段
Wordpress: How to make unique field with ACF and custom post type
所以我正在研究自定义 post 类型以与高级自定义字段一起使用。我有这个小脚本,可以阻止输入重复的电子邮件。但是脚本没有按照我的意愿运行。
您在下面看到的代码适用于我已链接到 ACF 的自定义 post 类型页面。
function init_members() {
$labels = array(
'name' => 'Members',
'singular_name' => 'Member',
'menu_name' => 'Members',
'name_admin_bar' => 'Member',
'add_new' => 'New member',
'add_new_item' => 'New member',
'new_item' => 'New member',
'edit_item' => 'Edit member',
'all_items' => 'All members',
'search_items' => 'Search member',
'not_found' => 'No members found',
'not_found_in_trash' => 'No members found in trash'
);
$args = array(
'labels' => $labels,
'public' => true,
'exclude_from_search' => true,
'rewrite' => array('slug' => 'member'),
'has_archive' => false,
'supports' => array('title'),
'show_in_rest' => true,
'menu_icon' => 'dashicons-groups'
);
register_post_type('members', $args);
}
add_action('init', 'init_members');
function add_member_columns ( $columns ) {
unset($columns['date']);
return array_merge ( $columns, array (
'contactperson' => __ ( 'Contactperson' ),
'phone_number' => __ ( 'Phonenumber' ),
'email' => __ ( 'Email' ),
) );
}
add_filter ('manage_members_posts_columns', 'add_member_columns' );
function fill_member_columns ( $column, $post_id ) {
switch ( $column ) {
case 'contactperson':
echo get_post_meta ( $post_id, 'contactperson', true );
break;
case 'phone_number':
echo get_post_meta ( $post_id, 'phone_number', true );
break;
case 'email':
echo get_post_meta ( $post_id, 'email', true );
break;
}
}
add_action ('manage_members_posts_custom_column', 'fill_member_columns', 10, 2 );
所以我尝试过滤重复电子邮件的脚本如下。
我得到它的来源是 here
add_filter('acf/validate_value/name='.'email', 'acf_unique_value_field', 10, 4);
function acf_unique_value_field($valid, $value, $field, $input) {
if (!$valid || (!isset($_POST['post_ID']) && !isset($_POST['post_id']))) {
return $valid;
}
if (isset($_POST['post_ID'])) {
$post_id = intval($_POST['post_ID']);
} else {
$post_id = intval($_POST['post_id']);
}
if (!$post_id) {
return $valid;
}
$post_type = get_post_type($post_id);
$field_name = $field['name'];
$args = array(
'post_type' => $post_type,
'post_status' => 'publish, draft, trash',
'post__not_in' => array($post_id),
'meta_query' => array(
array(
'key' => 'email',
'value' => $value
)
)
);
$query = new WP_Query($args);
if (count($query->posts)){
return 'This Value is not Unique. Please enter a unique '.$field['label'];
}
return true;
}
我可能监督了一些非常明显的事情。但是我无法找到我正在监督的内容。
我认为问题可能在于您没有查询 所有 个现有的 post。如果您的 posts 每页默认配置为 5,而第 6 个 post 结果与更新后的邮件地址相同,您的检查将找不到任何匹配的 posts .
尝试以下操作:在 acf_unique_value_field()
中,像这样修正您的查询:
$args = array(
'post_type' => $post_type,
'post_status' => 'publish, draft, trash',
'post__not_in' => array($post_id),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'email',
'value' => $value
)
)
);
$query = new WP_Query($args);
'posts_per_page' => -1,
将查询无限数量的 posts。
另外,我看到您正在使用 get_post_meta
来检索 ACF 字段值。我建议不要这样做。改用 ACF 的 get_field
(必要时使用 update_field
进行更新)。
所以我正在研究自定义 post 类型以与高级自定义字段一起使用。我有这个小脚本,可以阻止输入重复的电子邮件。但是脚本没有按照我的意愿运行。
您在下面看到的代码适用于我已链接到 ACF 的自定义 post 类型页面。
function init_members() {
$labels = array(
'name' => 'Members',
'singular_name' => 'Member',
'menu_name' => 'Members',
'name_admin_bar' => 'Member',
'add_new' => 'New member',
'add_new_item' => 'New member',
'new_item' => 'New member',
'edit_item' => 'Edit member',
'all_items' => 'All members',
'search_items' => 'Search member',
'not_found' => 'No members found',
'not_found_in_trash' => 'No members found in trash'
);
$args = array(
'labels' => $labels,
'public' => true,
'exclude_from_search' => true,
'rewrite' => array('slug' => 'member'),
'has_archive' => false,
'supports' => array('title'),
'show_in_rest' => true,
'menu_icon' => 'dashicons-groups'
);
register_post_type('members', $args);
}
add_action('init', 'init_members');
function add_member_columns ( $columns ) {
unset($columns['date']);
return array_merge ( $columns, array (
'contactperson' => __ ( 'Contactperson' ),
'phone_number' => __ ( 'Phonenumber' ),
'email' => __ ( 'Email' ),
) );
}
add_filter ('manage_members_posts_columns', 'add_member_columns' );
function fill_member_columns ( $column, $post_id ) {
switch ( $column ) {
case 'contactperson':
echo get_post_meta ( $post_id, 'contactperson', true );
break;
case 'phone_number':
echo get_post_meta ( $post_id, 'phone_number', true );
break;
case 'email':
echo get_post_meta ( $post_id, 'email', true );
break;
}
}
add_action ('manage_members_posts_custom_column', 'fill_member_columns', 10, 2 );
所以我尝试过滤重复电子邮件的脚本如下。 我得到它的来源是 here
add_filter('acf/validate_value/name='.'email', 'acf_unique_value_field', 10, 4);
function acf_unique_value_field($valid, $value, $field, $input) {
if (!$valid || (!isset($_POST['post_ID']) && !isset($_POST['post_id']))) {
return $valid;
}
if (isset($_POST['post_ID'])) {
$post_id = intval($_POST['post_ID']);
} else {
$post_id = intval($_POST['post_id']);
}
if (!$post_id) {
return $valid;
}
$post_type = get_post_type($post_id);
$field_name = $field['name'];
$args = array(
'post_type' => $post_type,
'post_status' => 'publish, draft, trash',
'post__not_in' => array($post_id),
'meta_query' => array(
array(
'key' => 'email',
'value' => $value
)
)
);
$query = new WP_Query($args);
if (count($query->posts)){
return 'This Value is not Unique. Please enter a unique '.$field['label'];
}
return true;
}
我可能监督了一些非常明显的事情。但是我无法找到我正在监督的内容。
我认为问题可能在于您没有查询 所有 个现有的 post。如果您的 posts 每页默认配置为 5,而第 6 个 post 结果与更新后的邮件地址相同,您的检查将找不到任何匹配的 posts .
尝试以下操作:在 acf_unique_value_field()
中,像这样修正您的查询:
$args = array(
'post_type' => $post_type,
'post_status' => 'publish, draft, trash',
'post__not_in' => array($post_id),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'email',
'value' => $value
)
)
);
$query = new WP_Query($args);
'posts_per_page' => -1,
将查询无限数量的 posts。
另外,我看到您正在使用 get_post_meta
来检索 ACF 字段值。我建议不要这样做。改用 ACF 的 get_field
(必要时使用 update_field
进行更新)。