Home Forums Front-end Issues Unique value field


Unique value field

  • Hi,
    I created a front-end form that uses this function mentioned here to prevent repeated entries. It works just fine on dashboard but it doesn’t work at all on front-end form.
    Am i doing something wrong or it was really not supposed to work on front-end forms?

    thank you!

  • Can you show your code for the front-end form? If you’re creating a new post, post_id will actually be a string of ‘new_post’ and will always fail the first few conditionals. Additionally, it could pick up the global $post of the page that the form is on which may not align with what you’re trying to do.

  • 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'])) {
            $post_id = intval($_REQUEST['item_id']);
        } else { 
            $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'];
  • Hopefully you’re not trying to update based off the invented post ID – If you can you should let WordPress create IDs for new posts ( which it does in MySQL ) otherwise you’ll surely run into issues down the road.

    Difficult to suggest the best course of action without the context of the rest of the code block.

  • As the writer of the original code, I’ve never tried it on a front end post.

    If the form is editing and existing post then a post ID should be set and the code should work.

    However, if this is for creating a new post, as @howdy_mcgee says, you’ll have issues.
    1) You can’t check for it

    2) Using a made up post ID will not work since the code requires the post_type and with a made up ID or an id of ‘new_?’ get_post_type() will return false.

    In the case of #2,
    1) you need to forget about checking to see if the post id is set and just remove that code.
    2) You need to hard code the post type(s) to check and not rely on get_post_type()
    3) You need to remove the 'post__not_in' => array($post_id), portion of the query since being a new post you don’t need to worry about the current post returning a false positive.

  • 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(
              'key' => $field_name,
              'value' => $value
        $query = new WP_Query($args);
            if( $query->have_posts() ) {
                $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';
            '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 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.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Unique value field’ is closed to new replies.