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
5 Answers
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.
- 2should we use $wpdb_prepare to prepare escape the SQL statement?insidepower– insidepower2015-03-14 07:09:17 +00:00Commented Mar 14, 2015 at 7:09
- Any chance to modify the code to only target a specific custom post type?Bruno Monteiro– Bruno Monteiro2016-03-29 12:58:47 +00:00Commented Mar 29, 2016 at 12:58
- Maybe
if(the_slug_exists('contact') && post_type_exists('contact'))but then why not only useif(post_type_exists('contact')) ...Jonas Lundman– Jonas Lundman2017-10-07 15:45:52 +00:00Commented Oct 7, 2017 at 15:45 - 1Using WP_Query() is a more maintainable solution than a direct DB query. For example,
$query = new WP_Query(array('name' => 'my-new-slug')); if ($query->post_count == 0) {// I am unique!}Alex Steinberg– Alex Steinberg2018-06-24 15:30:31 +00:00Commented Jun 24, 2018 at 15:30 - 1It can be written like this to use the correct prefix:
("SELECT post_name FROM {$wpdb->prefix}posts WHERE post_name = '$post_name'", 'ARRAY_A'))brasofilo– brasofilo2020-11-10 21:46:53 +00:00Commented Nov 10, 2020 at 21:46
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.
- 1sorry 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– lulalala2011-08-22 01:43:29 +00:00Commented Aug 22, 2011 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– Nathan2014-05-14 23:20:38 +00:00Commented May 14, 2014 at 23:20
How about this simpler approach?
$post_exists = get_page_by_path( $slug, OBJECT, $post_type ); if ( ! $post_exists ) echo 'No post exists with this slug.'; If a post doesn't exist for the given slug and post type provided then get_page_by_path() returns null.
- Note that path is not quite the same thing as a slug (or
post_nameto use the name of the database column). If a post has a parent post, then its path will beparent_slug/slugFlimm– Flimm2020-09-02 15:35:37 +00:00Commented Sep 2, 2020 at 15:35
$args = array('name' => $slugName, 'post_type' => $postType); $slug_query = new WP_Query($args); echo "<pre>"; var_dump($slug_query); exit; You then have more than enough information to test if a post was returned or not, hope this helps.
- 2How this will extract slug uniqueness?Rahil Wazir– Rahil Wazir2014-02-12 21:09:04 +00:00Commented Feb 12, 2014 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 ) { //............. } - 1Please add proper explanation to your answers like what the code does and how it should be usedPieter Goosen– Pieter Goosen2015-11-09 04:24:38 +00:00Commented Nov 9, 2015 at 4:24
if(is_page('slug-here')) { echo 'exists'; }.