我想要我的acf字段" unique_field"在我的自定义帖子类型"地点"独一无二。
我使用了此处记录的acf / validate_value - > https://www.advancedcustomfields.com/resources/acf-validate_value/ https://support.advancedcustomfields.com/forums/topic/solved-check-if-value-already-exist-2/
我试图将其放在我的functions.php
上,但它不起作用。
add_filter('acf/validate_value/name=unique_field', 'validate_unique_field_filter', 10, 4);
function validate_unique_field_filter($valid, $value, $field, $input) {
if (!$valid || $value == '') {
return $valid;
}
// query posts for the same value
global $post;
$args = array(
'post_type' => 'places',
'post__not_in' => array($post->ID),
'meta_query' => array(
array(
'key' => 'unique_field',
'value' => $value
)
)
);
$query = new WP_Query($args);
if (count($query->posts)) {
$valid = 'Place already exist';
}
return $valid;
}
有任何错误吗?请指教。谢谢!