Created
November 24, 2009 18:24
-
-
Save felipelavinz/242087 to your computer and use it in GitHub Desktop.
Get stuff by name (WordPress)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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