BPOVIA Website Design

Icon

Provides High-quality Web Design And Maintenance Service In The World

WordPress funcition: get page ID by slug

Issue: Many blogs are using postname as the permanent link, it would be hard to check for the page ID, of course it's easy for WordPress savvy, who can edit links from backend. However many users feel it troublesome and would like to type in page title or alias directly.

Analysis: For instance, catalog has the function to obtain id from catalog name (not slug), which is get_cat_ID, then we can create a function as follows:

function get_page_id($page_name){
    global $wpdb;
    $page_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."' AND post_status = 'publish' AND post_type = 'page'");
    return $page_name;
}

BPOVIA All Rights Reserved
Maintained by BPOVIA Web Design Team

Category: website, WordPress

Tagged: , ,

Comments are closed.