Home › Forums › Front-end Issues › Unique value field › Reply To: Unique value field
When editing, the ID comes form the url (REQUEST) and the invented ID disappears when the post is saved with its real new id.
I’d still be glad with a more elegant solution.
How could I let wp create a new ID itself before the post is saved or even exists?
here is the complete code of this function:
add_filter('acf/validate_value/name=fld_pubdoi_doinumber', 'acf_unique_value_field_pubs', 10, 4);
add_filter('acf/validate_value/name='.$field_name, 'acf_unique_value_field_pubs', 10, 4);
function acf_unique_value_field_pubs($valid, $value, $field, $input) {
if (!$valid ) {
return $valid;
}
if (isset($_POST['post_ID'])) {
$post_id = intval($_POST['post_ID']);
} elseif (isset($_POST['post_id'])) {
$post_id = intval($_POST['post_id']);
} elseif (isset($_REQUEST['item_id'])) {//IIQINST ONLY
$post_id = intval($_REQUEST['item_id']);
} else {
$post_id = intval('9999999999999999');
}
//$post_type = get_post_type($post_id);
$post_type = 'pt_publicacoes_doi';
$field_name = $field['name'];
$args = array(
'post_type' => $post_type,
'post_status' => 'publish, draft',
'post__not_in' => array($post_id),
'meta_query' => array(
array(
'key' => $field_name,
'value' => $value
)
)
);
$query = new WP_Query($args);
if( $query->have_posts() ) {
$query->the_post();
$another_post_id = get_the_ID();
}
if (count($query->posts)){
return $field['label'].' already exists';
}
return true;
}
And the form args:
$item_id = ( isset($_GET['item_id']) ) ? $_GET['item_id'] : 'new_post';
acf_form(array(
'id' => 'iiq-addedit-pub-form',
'post_id' => $item_id,
'post_title' => false,
'post_content' => false,
'form_attributes' => array(),
'submit_value' => __('SAVE', 'acf'),
'html_submit_button' => '<input type="submit" class="but-form" value="%s" />',
'new_post' => array(
'post_type' => 'pt_publicacoes_doi',
'post_status' => "publish"
),
'updated_message' => __($text_return, 'acf'),
'html_updated_message' => '<div id="adm-message" class="updated"><p>%s</p></div>',
'return' => $return_url,
));
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ποΈ Just one more day until the next session of ACF Chat Fridays! Make sure to register for the latest updates on whatβs coming in ACF 6.1!
— Advanced Custom Fields (@wp_acf) March 30, 2023
π Friday 31st March 3pm UTC
π Register here - https://t.co/3UtvQbDwNm pic.twitter.com/7xtEJakeQN
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.