Skip to content

Instantly share code, notes, and snippets.

@felipelavinz
Created November 24, 2009 18:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipelavinz/242087 to your computer and use it in GitHub Desktop.
Save felipelavinz/242087 to your computer and use it in GitHub Desktop.
Get stuff by name (WordPress)
<?php
/**
* Get stuff ID by nicename
* @param $nicename string The nicename (i.e: post-slug) to get the ID for
* @param $type string The table that will be queried: posts (default), term, users
* @return integer The ID for the requested object, false if nothing found
*/
function get_by_name($nicename, $type="posts"){
global $wpdb;
if ( empty($type) OR $type === 'post'){
$query = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = $nicename");
} elseif ( $type === 'term' ) {
$query = $wpdb->get_var("SELECT term_id FROM $wpdb->terms WHERE slug = $nicename");
} elseif ( $type === 'user' ) {
$query = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_login = $nicename");
}
if ( $query ) return $query;
else return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment