Support

Account

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,
          
        ));