Hi John, thanks for your reply and for your code!
After all, I think this is what I did (hard code the post_type), except for removing the 'post__not_in' => array($post_id),
just because my form can both edit and create a post.
So I wasn’t that wrong, was I?
Actually, it worked that way but I’m afraid that it could have a flaw and I am not seeing it.
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,
));
Hi Howdy_McGee, thanks a lot for your reply.
At some point I figured it out that the problem was the missing id for new post.
So I did the following:
(probably not the best idea, but…)
(...)
if (isset($_POST['post_ID'])) {
//just like it was at first
$post_id = intval($_POST['post_ID']);
} elseif (isset($_POST['post_id'])) {
//just like it was at first
$post_id = intval($_POST['post_id']);
} elseif (isset($_REQUEST['item_id'])) {
// BECAUSE MY FORM COULD BE EDITING INSTEAD OF INCLUDING
$post_id = intval($_REQUEST['item_id']);
} else {
// I INVENTED AN ID FOR THIS NEW POST
$post_id = intval('9999999999999999');
}
//And since I couldnt check the post-type for a new post,
//I specified the post-type here (cause in my case this function only applies to this form)
$post_type = 'pt_publicacoes_doi';
$field_name = $field['name'];
(...)
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.