Support

Account

Home Forums ACF PRO Slug validation

Solved

Slug validation

  • I have a validation function which checks for duplicate slugs.
    It works, but I forgot to include the ‘current post’, so now it always returns false, since the current post is not excluded.

    This is my code.

    function pg_check_title( $valid, $value, $field, $input ) {
    
        if ( ! $valid ) {
            return $valid;
        }
    
        $post_name = sanitize_title( pg_replace_special_characters( $value ) );
    
        if ( true == pg_slug_exists( $post_name, DpgPostTypes::LONGREAD ) ) {
            $valid = __( 'This title already exists for a longread.', 'dpg' );
        } elseif ( true == pg_slug_exists( $post_name, DpgPostTypes::TEMPLATE ) ) {
            $valid = __( 'This title already exists for a template.', 'dpg' );
        }
        
        return $valid;
        
    }
    add_filter( 'acf/validate_value/key=field_5ae091e84cb7b', 'pg_check_title', 10, 4 );
    
    function pg_slug_exists( $post_name, $post_type ) {
        global $wpdb;
        if ( $wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "' AND post_type = '" . $post_type . "'", 'ARRAY_A' ) ) {
            return true;
        } else {
            return false;
        }
    }

    I want to add a post id, so I can exclude this ID in my sql query.

    As soon as I add a post ID to the mix, I don’t get the stylish error notice but a grey screen with a text error.

    I add an ID to the function pg_slug_exists and then the grey screen comes, like below.

    if ( true == pg_slug_exists( $post_name, DpgPostTypes::LONGREAD, $_GET[ 'id' ] ) ) {
        $valid = __( 'This title already exists for a longread.', 'dpg' );
    } elseif ( true == pg_slug_exists( $post_name, DpgPostTypes::TEMPLATE, $_GET[ 'id' ] ) ) {
        $valid = __( 'This title already exists for a template.', 'dpg' );
    }

    Why don’t I see the stylish error ?

  • I also tried this:

    function pg_slug_exists( $post_name, $post_type ) {
        global $wpdb;
        $exclude = false;
        if ( isset( $_GET ) && isset( $_GET[ 'id' ] ) ) {
            $exclude = " AND ID != '".$_GET[ 'id' ]."'";
        }
        if( $wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "' AND post_type = '" . $post_type . "'".$exclude."", 'ARRAY_A' ) ) {
            return true;
        } else {
            return false;
        }
    }

    but no luck

  • I was just able to finish the function how I expect it to work…

    I think the issue was where I use $_GET[‘id’] I should have used $_POST[‘_acf_post_id’] because with $_POST it works as expected.

    This is the full working result:

    function pg_check_title( $valid, $value, $field, $input ) {
    
        if ( ! $valid ) {
            return $valid;
        }
    
        $post_name = sanitize_title( pg_replace_special_characters( $value ) );
    
        if ( true == pg_slug_exists( $post_name, DpgPostTypes::LONGREAD, $_POST[ '_acf_post_id' ] ) ) {
            $valid = __( 'This title already exists for a longread.', 'dpg' );
        } elseif ( true == pg_slug_exists( $post_name, DpgPostTypes::TEMPLATE, $_POST[ '_acf_post_id' ] ) ) {
            $valid = __( 'This title already exists for a template.', 'dpg' );
        }
        
        return $valid;
        
    }
    add_filter( 'acf/validate_value/key=field_5ae091e84cb7b', 'pg_check_title', 10, 4 );
    
    function pg_slug_exists( $post_name, $post_type, $post_id ) {
        global $wpdb;
        $exclude = false;
        if ( false != $post_id ) {
            $exclude = " AND ID != '" . $post_id . "'";
        }
    
        if ( $wpdb->get_row( "SELECT post_name, ID FROM wp_posts WHERE post_name = '" . $post_name . "' AND post_type = '" . $post_type . "'" . $exclude . "", 'ARRAY_A' ) ) {
            return true;
        } else {
            return false;
        }
    }
  • Hmm… i cheered too soon… local it works without a hitch…

    On production I still get the ‘grey error’, so back to zero 🙂

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

You must be logged in to reply to this topic.