Wordpress:检查在特定的自定义帖子类型中是否存在slug


Wordpress: Check if slug exists in specific custom post type

我使用下面的代码来检查是否存在slug,但它正在搜索所有帖子类型,我只需要检查特定的自定义帖子类型。

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

用法:

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

是否可以修改此代码以仅搜索特定的自定义帖子类型?

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

使用

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