mercredi 14 septembre 2016

Check if value exist and add +1 in slug

I have written a code for checking my wordpress db if a slug exist. Now i have a problem with the if.

So far i got this:

$slug = sanitize_title_with_dashes( $posttitel ). sanitize_title_with_dashes( $bildungseinrichtung_name );
    // Check if slug exist
    $post_if = $wpdb->get_var("SELECT count(post_title) FROM $wpdb->posts WHERE post_name like '".$slug."'");
    // If not fire it in the DB
    if ($post_if < 1) {
        $data['post_name'] = $slug;
    }
    // If it exist add 1
    else {
        $post_plus = $post_if++;
        $data['post_name'] = $slug.'-'.$post_plus;
    }

So everything works fine for the first post.

Like:

post-one and post-one-1

After that it start again with the slug test got there back one exist and set again 1. But now i have two post with post-one-1.

So what i want is that he add every post-slug if exist the latest +1.

Thanks

Aucun commentaire:

Enregistrer un commentaire