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

  • |

    Why the WordPress.com / Spotify Deal is Bad for Podcasting

    Imagine telling Gordon Ramsay that he cooked dinner last night…why not just serve the leftovers to his customers today? While I’m no Michelin chef, I do create a lot of content. And you can’t just throw the left overs out there and expect the same quality content. So why are WordPress.com and Spotify telling us to do that?

  • |

    What is a Hackathon?

    According to Wikipedia, a hackathon is defined as: …an event in which computer programmers and others involved in software development, including graphic designers, interface designers and project managers, collaborate intensively on software projects. Basically, it’s a place were people who work with computers can come together to hopefully make something awesome. There are all types…

  • |

    Why Not Wanting to Pay for Software is Holding Your Business Back

    Earlier this month, Elementor announced that they are increasing their prices. Most notably, 1000 sites (called the Agency Plan) is moving from $199/year to $999/year. This is only for new customers but, as is common when software pricing changes, people are outraged. Not only is this sort of outrage getting old, but I would argue that if you’re not willing to pay what software is worth, you will never grow as a business. And not for the reason you think.

  • Projects Roundup for February 7th.

    I’ve had quite a productive week as far as new work, releases, and new projects go and I’d like to share them here on the blog. A few weeks ago I made a decision to revamp the way I work, learn new things, and even adjusted my work schedule a bit to make me more…

  • |

    If You’re Afraid of Automattic Making $5K Websites, You Need to Change Your Approach

    Recently, WordPress.com/Automattic “entered” the sub-$5,000 website market by announcing a new program, as reported by WordPress Tavern. Naturally, the reaction was mixed, but there seems to be a lot of anger that Automattic would do such a thing, which could potentially kill competition because it’s so big. Here’s the thing: If you think you’re competing with Automattic for sub-$5,000 websites, you need to change your approach.

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.