Quick Tip: Check to See if a Slug Exists in WordPress

Recently I was doing some work where I was importing RSS entries into a Custom Post Type in WordPress. Since there were no common IDs between the feed and WordPress, to prevent duplicate entries I tried comparing titles. This also proved to be an issue as titles aren’t always unique, and they weren’t in this case. I settled on comparing slugs; WordPress creates a unique slug for each post from the title, and there’s a way to know what that slug is going to be before the post is added.

Assuming that our RSS entries are in an Object called $item, we do this to get the slug:

$title= wp_strip_all_tags($item->title);
 $slug = sanitize_title($title);

The first function will remove all HTML tags from the title that was passed in; it was unclear if the next function, sanitize_title(), did that, so I took the extra precaution.

sanitize_title() is where the magic happens. It takes a now HTML-less string and converts it to a string that can be used in URLs; this is the would-be slug. You can then pass that slug to this query to see if a post with this slug already exists:

$post_if = $wpdb->get_var("SELECT count(post_title) FROM $wpdb->posts WHERE post_name like '".$slug."'");

If $post_if is < 1 at this point, the post doesn’t exist. Do with it what you will! If that’s the specific slug you want to use, you can also add it to the post array like this(assuming you’re using $post_array):

$post_array['post_name'] = $slug;

And there you have it! Feel free to comment your thoughts, improvements, and clarifications.

Similar Posts

  • |

    How to Easily Search Your Tweets in Chrome

    Since moving back to Chrome, I’ve missed some of my custom searches in the Keyword Search extension for Safari, despite having the same support in TextExpander. It just didn’t feel as fast as native browser support. Luckily, Chrome’s search shortcuts are fantastic and super easy to manage from chrome://settings/searchEngines1. Did you know you can do…

  • I’m Speaking at WordCamp Baltimore!

    The last WordCamp I went to was WordCamp Chicago in June, with it rounding out a big tour that started in January to promote my book, Responsive Design with WordPress. While I took a much needed break (and started a new job thanks in part to that trip), I’m really excited to announce that I’ll…

  • |

    WP Engine Easter Egg Saves You 20%

    I got a hot tip in my inbox today from one of my favorite hosting companies, WP Engine. For a limited time, when you visit their website and put in the Konami Code (below), you’ll save 20% on your purchase. To activate this discount, enter the following code using arrows and letters on their keyboard when…

  • |

    Adding the Media Uploader in WordPress

    …without including the Editor. In the Admin. That was a really long title, so I hope you don’t feel mislead! I was recently working on a project that required a Custom Post Type without the editor, but needed the Media Uploader. Note: This is not a full-blown tutorial. The purpose of this post is to…

  • I’m Speaking about WordPress on Saturday in Scranton!

    Come on down to the Vintage Theater at 9am Saturday to see me speak about the basics of WordPress! My talk is part of a 24 Hour bonanza that’s going on from Friday at 9pm to Saturday at 9pm. I will include art, music, movies, tech, and more! The Vintage Theater is located at 326 Spruce…

  • The way to learn WordPress is not by contributing

    has happened in WordPress this year, and what we can expect moving forward. As always, he extolled the virtues of open source and the importance of contributing. He spent several minutes on Five for the Future, a program encouraging companies who make money with WordPress to give 5% of their time to the open source project. And while I strongly recommend contributing, I don’t think it’s the path someone should take when they first learn.

One Comment

  1. Savior! A decade later your post_if snippet still works. Does what is_page() should be doing imho, which is check the entire site for presence of a slug, instead of only the one being visited.

Comments are closed.