【问题标题】:Wordpress: Check if slug exists in specific custom post typeWordpress:检查特定自定义帖子类型中是否存在 slug
【发布时间】:2023-03-20 21:47:01
【问题描述】:

我正在使用下面的代码检查是否存在 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;

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

【问题讨论】:

  • 你试过WHERE post_name = '" . $post_name . "' AND post_type = '" . $post_type . "'"吗?
  • 谢谢@Chay22。这正是我需要的。请回复此 sn-p 作为答案,以便我可以关闭此线程。

标签: php mysql wordpress


【解决方案1】:
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;

【讨论】:

    猜你喜欢
    • 2017-01-31
    • 2017-03-30
    • 2017-02-28
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多