Hajer
Hajer

Reputation: 13

Get page Id from slug in wordpress

I want to get page Id from slug. I used the function

$page = get_page_by_path("page-slug", OBJECT, 'page');

But it returns media attachment instead of page. I only wants the pages not any other post type.

Upvotes: 1

Views: 15396

Answers (3)

samjco-com
samjco-com

Reputation: 409

Here you go! Referenced from: https://gist.github.com/matheuseduardo/11f258d0895dec5885c8

/**
* Retrieve a page given its slug.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string       $page_slug  Page slug
* @param string       $output     Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A.
*                                 Default OBJECT.
* @param string|array $post_type  Optional. Post type or array of post types. Default 'page'.
* @return WP_Post|null WP_Post on success or null on failure
*/
function get_page_by_slug( $page_slug, $output = OBJECT, $post_type = 'page' ) {
    global $wpdb;

    if ( is_array( $post_type ) ) {
        $post_type = esc_sql( $post_type );
        $post_type_in_string = "'" . implode( "','", $post_type ) . "'";
        $sql = $wpdb->prepare( "
            SELECT ID
            FROM $wpdb->posts
            WHERE post_name = %s
            AND post_type IN ($post_type_in_string)
        ", $page_slug );
    } else {
        $sql = $wpdb->prepare( "
            SELECT ID
            FROM $wpdb->posts
            WHERE post_name = %s
            AND post_type = %s
        ", $page_slug, $post_type );
    }

    $page = $wpdb->get_var( $sql );

    if ( $page )
        return get_post( $page, $output );

    return null;
}

Now the get_post function will return an array object. SO you can pick from the parameters:

WP_Post Object
(
    [ID] =>
    [post_author] =>
    [post_date] => 
    [post_date_gmt] => 
    [post_content] => 
    [post_title] => 
    [post_excerpt] => 
    [post_status] =>
    [comment_status] =>
    [ping_status] => 
    [post_password] => 
    [post_name] =>
    [to_ping] => 
    [pinged] => 
    [post_modified] => 
    [post_modified_gmt] =>
    [post_content_filtered] => 
    [post_parent] => 
    [guid] => 
    [menu_order] =>
    [post_type] =>
    [post_mime_type] => 
    [comment_count] =>
    [filter] =>
)

So you can retrieve just ID amongst other things from slug by using the function in your template file:

$post_obj = get_page_by_slug('this-is-my-slug', OBJECT, 'post') // <-- change the posttype 
$post_id = $post_obj->ID;

echo $post_id; //id
echo $post_obj->ID; // id

// Or other things:
echo $post_obj->post_title; //Post Title
echo $post_obj->post_content; // Post Content

Or want the alternate output? Use ARRAY_A.

$post_obj = get_page_by_slug('this-is-my-slug', ARRAY_A, 'post' );
$post_id= $post_obj['ID'];

echo $post_id; //id
echo $post_obj['ID']; // id

// Or other things:
echo $post_obj['post_title']; //Post Title
echo $post_obj['post_content']; // Post Content

Upvotes: 1

interdevel
interdevel

Reputation: 31

To avoid getting attachments, pass an array containing only ‘page’ as third parameter, as follows:

$page = get_page_by_path( "page-slug", OBJECT, array( 'page' ) );

I saw this at https://developer.wordpress.org/reference/functions/get_page_by_path/#comment-3046

Upvotes: 3

Parth Shah
Parth Shah

Reputation: 411

Try this function

function get_id_by_slug($page_slug) {
    // $page_slug = "parent-page"; in case of parent page
    // $page_slug = "parent-page/sub-page"; in case of inner page
    $page = get_page_by_path($page_slug);
    if ($page) {
        return $page->ID;
    } else {
        return null;
    }
} 

Upvotes: 2

Related Questions