Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress: Check if slug exists in specific custom post type

I'm using the code below to check if a slug exists, but it's searching on all post types and I need to check only on a specific custom post type.

function the_slug_exists($post_name) {
    global $wpdb;
    if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "'", 'ARRAY_A')) {
        return true;
    } else {
        return false;
    }
}

Usage:

if (the_slug_exists($term)) :
    echo 'Ok';
endif;

Is it possible to modify this code to search only on a specific custom post type?

like image 648
Bruno Monteiro Avatar asked Oct 18 '22 12:10

Bruno Monteiro


1 Answers

function the_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;
    }
}

Usage

if (the_slug_exists($term,$type)) :
    echo 'Ok';
endif;
like image 158
Chay22 Avatar answered Oct 21 '22 06:10

Chay22