WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a arbitrary string and want to check if a post with that slug string exists in the site. I tried to find a way to list all slugs, but can't find such a thing. Thanks

share|improve this question
The question is protected, so i can't reply. You can use if(is_page('slug-here')) { echo 'exists'; }. – Iago Melanias Jul 5 '15 at 12:43

Do you mean post slug? You can try to make use of wp_unique_post_slug() that WP uses to generate those. If I remember right if slug you are trying to use is not unique it will be returned with numerical index appended.

share|improve this answer
sorry to reply so late. Yes post slug is the slug I want. Unfortunately I don't have the post_id which is required to use wp_unique_post_slug() – lulalala Aug 22 '11 at 1:43
I couldn't figure out how to use wp_unique_post_slug to do this anyway. It does really check to see if a slug exists, as much as create one. I could be wrong or misinterpreting something but in my testing it would not check if something already existed. – Nathan May 14 '14 at 23:20

This is what you're looking for, tested and I use it on my own sites:

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;

You can then use it like this:

if (the_slug_exists('contact')) {
    // do something

Replace contact with whatever slug you want to test for.

share|improve this answer
should we use $wpdb_prepare to prepare escape the SQL statement? – insidepower Mar 14 '15 at 7:09
Any chance to modify the code to only target a specific custom post type? – Bruno Monteiro Mar 29 at 12:58
$args = array('name' => $slugName, 'post_type' => $postType); 

$slug_query = new WP_Query($args);
echo "<pre>";

You then have more than enough information to test if a post was returned or not, hope this helps.

share|improve this answer
How this will extract slug uniqueness? – rahilwazir Feb 12 '14 at 21:09
$your_slug = 'my-pageeeeeee';
$wpdb = $GLOBALS['wpdb'];

//==================FIRST method======================
    $id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '%s' AND ( post_type = 'page' OR post_type = 'post') ", $slug) );
    if (!empty($id)) {

//====================SECOND method======================
    $counts = $wpdb->get_var($wpdb->prepare("SELECT count(post_name) FROM ".$wpdb->posts ." WHERE post_name like '%s'", $slug) );
    if ($counts >=1 ) {
share|improve this answer
Please add proper explanation to your answers like what the code does and how it should be used – Pieter Goosen Nov 9 '15 at 4:24

protected by Community Apr 10 '13 at 12:42

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.