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

  • | |

    Include a Sidebar with a Shortcode in WordPress

    Recently I was faced with a pretty interesting problem where I wanted to have a page that was structured: Content – Widgets – Content. Instead of hacking together some sort of Content – Sidebar – Metadata on the backend, I decided to make sidebar widgets accessible via a shortcode you can use in the WordPress…

  • |

    WordCamp Scranton Followup

    It’s been a little over a week since WordCamp Scranton, the dust as settled, and I have slept. First, I want to thank everyone who came out: attendees, speakers, sponsors, and volunteers. I also want to thank Johnson College for being such a great host. Finally, big thanks to Matt Mullenweg for taking time out…

  • |

    WordPress Cyber Weekend Deals 2018

    Well it’s that time of year, where there are lots of deals that I’d rather buy for myself than for others. I think nothing better encapsulates that than the digital deals going on in the WordPress /online worker space. I mean don’t get me wrong, I’d love to get a great deal on hosting. But…

  • |

    Win a Free Ticket to WP Summit!

    Last week I talked about the tail end of my speaking tour (thanks to everyone who came out to @WordCampPhilly). Up next is @WordCampChicago, but I’ll also be speaking at the WP Summit 2014, an online conference, on Wednesday, June 18th and I want to give away a free ticket!

  • 3 Ways to Make the Most of Your WordCamp US Experience

    WordCamp US 2019 is next week and I’m getting excited. It’s the biggest US-based conference for people in the WordPress community and at this point, it feels a lot like a college reunion to me. But there are also goals I’d like to accomplish while I’m there. If you’re spending your own money to get…

  • Interesting Links for July 2, 2013

    Google Reader Vigil: This one is a little shameless self-promotiony, but I created a site for users to pay their last respected to the new defunct Google Reader. Google Reader was my first RSS reader and showed me the true power of RSS. I used it faithfully for 8 years and I will deeply miss…

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.