Retrieve a page (and page ID) given its slug
/**
* Retrieve a page given its slug.
*
* @param string $pageSlug : Page slug
* @param string $output : Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
* @param string|array $postType : Optional. Post type or array of post types. Default 'page'.
*
* @return WP_Post|array|void : WP_Post on success or null on failure
*/
function get_page_by_slug($pageSlug, $output = OBJECT, $postType = 'page')
{
$pageID = get_page_id_by_slug($pageSlug, $postType);
if ($pageID) {
return get_post($pageID, $output);
}
}
/**
* Retrieve a page id given its slug.
*
* @global wpdb $wpdb : WordPress database abstraction object.
*
* @param string $pageSlug : Page slug
* @param string|array $postType : Optional. Post type or array of post types. Default 'page'.
*
* @return string|void : ID on success or null on failure
*/
function get_page_id_by_slug($pageSlug, $postType = 'page')
{
global $wpdb;
$language = ICL_LANGUAGE_CODE;
if (is_array($postType)) {
$postType = esc_sql($postType);
$postTypeInString = "'" . implode("','", $postType) . "'";
$sql = $wpdb->prepare("
SELECT ID
FROM $wpdb->posts
WHERE post_name = %s
AND post_type IN ($postTypeInString)
", $pageSlug);
} else {
$sql = $wpdb->prepare("
SELECT ID
FROM $wpdb->posts
WHERE post_name = %s
AND post_type = %s
", $pageSlug, $postType);
}
$id = $wpdb->get_var($sql);
if ($id) {
return $id;
// Use WPML filter to return the localized version
//return apply_filters('wpml_object_id', $id, 'page', true, $language);
}
}