mark.watero.us

Wordpress stuff, a statistics plugin, and jello

get_post_ID(), a function for finding yourself

2 comments

Following up on my post about the lack of an is_child() function in Wordpress, I wrote another simple function that I find myself wanting on a regular basis.

Almost every function that pulls information from the database about a specific post, category, or otherwise requires a numerical ID. Well, if you’re like me, and you develop a lot of client sites locally and on your own sandbox server before delivering it to the client, it’s a real pain in the arse to have to go through your code and change ID’s to make them current with the final product.

How come Wordpress doesn’t have get_post_ID()?

13 lines is all, and you can find any posts ID so long as you know what the slug is. This can be used in combination with any number of Wordpress functions to simplify a lot of calls. The only pitfall is that if you change a post/page slug your code will break, but in the case of pages, I’ve found this to be a rare occurrence anyways.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function get_post_ID ($the_name = '') {
	global $wpdb;
 
	if ( empty($the_name) )
		return FALSE;
 
	$the = $wpdb->get_results( "SELECT ID FROM `{$wpdb->posts}` WHERE post_name = '{$the_name}'", ARRAY_A );
 
	if ( isset($the[0]['ID']) && ! empty($the[0]['ID']) )
		return $the[0]['ID'];
 
	return FALSE;
 
}

Now that you can find the ID of your post, you can do whatever you want with it. For example if you want to link to a specific page somewhere in your template without having to call wp_list_pages(), you have easy access to the permalink via;

1
2
3
4
5
6
$the_id = get_post_ID('about');
echo '<a href="' . get_permalink($the_id) . '">About Us</a>';
 
// or if you prefer nested functions
 
echo get_permalink( get_post_ID('about') );

It’s not too hard to modify this to work with post titles instead of just post names (slugs), but since two pages/posts can have exactly the same title, but can never have the same slug, it’s safer to work with it the way it is.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • NewsVine
  • Reddit
  • StumbleUpon
  • Technorati
  • Twitter

Written by mark

September 23rd, 2009 at 5:24 pm

Posted in Code Snippets

Tagged with , ,

2 Responses to 'get_post_ID(), a function for finding yourself'

Subscribe to comments with RSS or TrackBack to 'get_post_ID(), a function for finding yourself'.

  1. Hey Mark, nice call. Although turns out WordPress includes a function get_the_ID() which can be used outside of the loop.

    Matt Cassarino

    14 Dec 09 at 222:29 pm

  2. @Matt,
    Didn’t see that one in my travels back then, thanks for pointing that out. :) If I get around to updating my old articles as I hope to do, I’ll include a reference to that function – when I wrote this I was still fairly new on the scene (still am technically) and just couldn’t find it for some reason.

    mark

    14 Dec 09 at 555:35 pm

Leave a Reply