WordPress-检查段塞可用性

WordPress-检查段塞可用性,wordpress,slug,Wordpress,Slug,如何检查我的WordPress数据库中是否已经存在slug 我想检查任何slug(post、page、分类法和自定义post类型/分类法) 谢谢有答案。如果你使用它给它想要的slug,它将返回一个真正独特的slug 我刚才在这里回答了这个问题: 不确定重复答案的政策是什么,但您可以这样做: function the_slug_exists($post_name) { global $wpdb; if($wpdb->get_row("SELECT post_name FROM

如何检查我的WordPress数据库中是否已经存在slug

我想检查任何slug(post、page、分类法和自定义post类型/分类法)


谢谢

有答案。如果你使用它给它想要的slug,它将返回一个真正独特的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('contact')) {
            // do something
    }
将“contact”替换为要测试的任何slug。


<?php
    $url = $_SERVER["REQUEST_URI"];
    $isItYourSlug = strpos($url, 'your_slug');

    if ($isItYourSlug!==false) {
        Do Something
    }
?>
还好吗?这对我有用

使用:


一般来说,
WP\u Query()
可以避免直接调用数据库,使您的站点更易于维护。

使用
WP\u unique\u post\u slug()
获得唯一slug。。。如果您的slug存在于,则此函数将为您提供后缀为(-2,-3,-4…)的新slug…该死!在我发布后看到了这个。@AkshayPaghdar它检查所有的slug(第页,cpt…)还是只检查post slug?THX完整的函数参数如下:-
wp\u unique\u post\u slug($slug,$post\u ID,$post\u status,$post\u type,$post\u parent)
因此您可以在此函数中提供post\u类型……好的方法只适用于默认安装,而wp\u前缀没有更改,这是您应该做的。这将起作用:
if($wpdb->get_row(“从“$wpdb->prefix.”选择post_name=”、“$post_name.”、“ARRAY_A”){…
$args = array(
  'post_type'    => 'custom-post-type',
  'name' => 'my-new-slug'
);

$query = new WP_Query($args);

if ($query->post_count == 0) {
  // I am unique!
}