Php Wordpress:检查特定自定义帖子类型中是否存在slug
我正在使用下面的代码来检查slug是否存在,但它正在搜索所有帖子类型,我只需要检查特定的自定义帖子类型Php Wordpress:检查特定自定义帖子类型中是否存在slug,php,mysql,wordpress,Php,Mysql,Wordpress,我正在使用下面的代码来检查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; } el
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;
}
}
用法
你试过
其中post_name='“$post_name.”和post_type='“$post_type.”“
?谢谢@Chay22。这正是我需要的。请用这个片段作为答案回复,这样我就可以关闭这个线程了。
if (the_slug_exists($term,$type)) :
echo 'Ok';
endif;