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

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

关于php - Wordpress:检查特定自定义帖子类型中是否存在 slug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36336236/

相关文章:

PHP Oracle 分页无法使用 ROWNUM 显示数据

javascript - 如何让用户返回到使用当前代码的特定 div

PHP 使用 MD5 密码登录?

mysql - 为什么 MySQL 显式谓词锁定不允许在谓词锁之外插入语句

wordpress - Docker 编写 php-fpm、nginx、mysql 并在 nginx 上使用 wp-cli

php - Woocommerce 评级未显示在新的自定义主题中

php - 如何存储实体的动态/多个属性

mysql - 导出mysql数据库上的多个表

即使传递了错误的值,MYSQL TRIGGER If 语句也会执行

php - 根据 Woocommerce 中的产品类别更改货币符号