• Resolved cody_r

    (@cody_r)


    I’m not sure why I’m having this problem, but for some reason I’m able to enter duplicate values on the frontend of my site. It works fine in the admin panel, but doesn’t work on the front end for some reason.

    I have the frontend validation checked in the settings, but it’s just not working.

    Here is my code:
    functions.php

    function my_pre_save_post( $post_id ) {
      if ( $post_id != 'new' ) {
        return $post_id;
        }
        if ( current_user_can('publish_posts') ) {
          $post = array(
              'post_status' => 'publish',
              'post_title' => $_POST['fields']['field_5458997dcb49c'],
              'post_type' => 'videos'
          );
          $post_id = wp_insert_post($post);
          $_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
          return $post_id;
        } else {
          $post = array(
              'post_status' => 'pending',
              'post_title' => $_POST['fields']['field_5458997dcb49c'],
              'post_type' => 'videos'
          );
          $post_id = wp_insert_post($post);
          $_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
          return $post_id;
        }
    }
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );

    theme template file

    <?php if ( is_user_logged_in() ) { ?>
            				<?php while ( have_posts() ) : the_post(); ?>
            					<?php acf_form(array(
            						'post_id'		=> 'new',
            						'field_groups' => array(8),
            						'new_post'		=> array(
            							'post_type'		=> 'videos',
            							'post_status'		=> 'publish'
            						),
            						'submit_value'		=> 'Share Video'
            					)); ?>
            				<?php endwhile; ?>
                    <?php } else { ?>
                        <p>You must be logged in to view this page!</p>
                    <?php } ?>

    https://ww.wp.xz.cn/plugins/validated-field-for-acf/

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter cody_r

    (@cody_r)

    I’m not sure if it’s worth noting, but the character limit is working on the frontend, just not uniqueness.

    Plugin Author doublesharp

    (@doublesharp)

    Hi @cody_r – can you let me know what version of WordPress, ACF, and Validated Field you are using? I’m also not sure which value you want to check for uniqueness (global, post type, type+key) and what post statuses you have selected to have it apply to. Is it possible to include a screenshot of your config?

    Thread Starter cody_r

    (@cody_r)

    Thank you for the fast response.

    I’m using the most up to date version of everything. It’s checking for uniqueness for post type. The statuses are published, scheduled, draft, and pending.

    Screenshots:
    http://prntscr.com/5315ig
    http://prntscr.com/5315m3
    http://prntscr.com/5315qe
    http://prntscr.com/5315uj

    Plugin Author doublesharp

    (@doublesharp)

    If you check the browser console do you have any javascript errors? The validation is over AJAX, so if there is then it might not be intercepting the submit correctly – the new version for ACF should validate this both ways.

    Thread Starter cody_r

    (@cody_r)

    This is the only JS error I’m getting: http://prntscr.com/534dgc

    Plugin Author doublesharp

    (@doublesharp)

    That’s probably what is blocking the AJAX. Not sure what the cause is, but it seems to be related to TinyMCE.

    Unfortunately with ACF 4 this plugin can only run via an AJAX request, and that is likely to not run if you encounter a javascript error.

    Thread Starter cody_r

    (@cody_r)

    What exactly could be causing the error? I don’t any plugins are conflicting..

    Plugin Author doublesharp

    (@doublesharp)

    Try turning on the PHP validation and just put return false; and see if that will make it fail, maybe it really is just a bug in the query 🙂

    Thread Starter cody_r

    (@cody_r)

    return false; is making it fail.

    Thread Starter cody_r

    (@cody_r)

    I changed it to Unique Globally and it seemed to fix it. Weird that is was just that this whole time.

    Plugin Author doublesharp

    (@doublesharp)

    I’m not totally sure that was the cause – I am doing some interesting SQL prepare()’ing for WPML compatibility and it is expecting a digit for the post_id, I’m wondering if that is not working because you passed in “new” for the ID. Supporting front end forms is somewhat new, and I don’t personally use it so it’s possible this was missed.

    Can you try it again with a new post on type + key, type, and global but with the post_id set to 0?

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Frontend unique for post type’ is closed to new replies.