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

  • |

    Videos from May 2019

    It’s been a crazy month, and I barely blogged, but that doesn’t mean I wasn’t pumping out content. Here’s a roundup of all the video (and other content I put out in May 2019.

  • Ok Ok

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!I know i havent posted in a while, but ive been really busy. If you havent noticed, the face of revenge…

  • |

    Upcoming News & Events

    Whoa. Almost a month has gone by and I haven’t written a thing on this blog; sorry about that! Since officially announcing my new book, Responsive Design with WordPress, I’ve been working diligently to finish up all of the writing, reviews, edits, and design approvals. I’m happy to say that Monday I will be done…

  • 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.

  • GitGutter for Sublime Makes Diffs Easy

    At Crowd Favorite we use git for everything, which is a nice change from my old workflow of hoping for the best. I’ve learned quite a bit about git and Github since starting and have looked for tools to help me do things better. A really simple, but super helpful, tool is the GitGutter package for…

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.