Code

Quick Function: Get a post’s slug in WordPress

Quick Function: Get a post’s slug in WordPress

Here’s another little function that tends to come in handy. Recently, on a project I did for a client, they had a sidebar where they wanted to display pages that were children of a main page. I whipped up this function to get the ID of a page’s slug and then perform a query:


function get_ID_by_slug($page_slug) {
   $page = get_page_by_path($page_slug);
   if ($page) {
      return $page->ID;
   } else {
      return null;
   }
}

So for example, you could then do a query like this in your sidebar:


$querystr = "SELECT wposts.* FROM $wpdb->posts wposts WHERE wposts.post_parent = '".get_ID_by_slug("white-papers")."' AND wposts.post_status = 'publish' AND wposts.post_type = 'page'";

And you would get all pages that are children of “white-papers”, it’s basic I know, but it works, and has a few other uses than just where I used it.

Post Author

This post was written by who has written 386 posts on Roger Stringer.

Roger Stringer is a web developer, writer and editor with over 10 years of experience in web development. Roger Stringer is the founder of sites such as TheInterviewr and DBStract and is the editor of sites such as FoodJumper, Foodizu and Attack Of Code.

No comments yet.

Leave a Reply