Question

I'm pretty new to WordPress and I was wondering if I could get a page id with its slug. Is it possible please let me know?

Était-ce utile?

La solution

Use get_page_by_path($page_path):

$page = get_page_by_path( 'about' );
echo get_the_title( $page );

This will return a regular post object.
Documentation:
https://developer.wordpress.org/reference/functions/get_page_by_path/
https://developer.wordpress.org/reference/functions/get_the_title/

Autres conseils

I've been using this ..

function get_id_by_slug($page_slug) {
    $page = get_page_by_path($page_slug);
    if ($page) {
        return $page->ID;
    } else {
        return null;
    }
} 

Hope this will help someone.

It has been already asked and answered on this forum. I am pasting the same code from there. Use this function to retrieve page id.

 function get_page_by_slug($page_slug, $output = OBJECT, $post_type = 'page' ) { 
  global $wpdb; 
   $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = 'publish'", $page_slug, $post_type ) ); 
     if ( $page ) 
        return get_post($page, $output); 
    return null; 
  }

I had problems with the chosen answer when trying to use the code several times in the same page. It kept on displaying all of my pages content at the same time in every instance. So I went back to thinking and came up with this simpler approach based on the WordPress Codex's documentation:

<?php $query = new WP_Query( array( 'pagename' => 'about-me' ) );
        while ( $query->have_posts() ) {
        $query->the_post();
        echo '<h2>'. get_the_title() .'</h2>';
        the_content();
    }
    wp_reset_postdata();
?>

Maybe it can still be helpful for somebody out there ;D

A lot of answers here that seem overly complex, or don't describe how to get the page ID specifically.

            $page = get_page_by_path("your-page-slug");
            if ($page) {
                $page_id =  $page->ID;
                echo $page_id;
            }

In the above description we've assigned the post object to $page - Once you have the post object you can get any of the info described here: https://codex.wordpress.org/Class_Reference/WP_Post

            $page->ID
            $page->post_status
            $page->post_title

and a lot more

There is a function url_to_postid since WordPress v1.0.0 :) This task is easiest to achieve by using this function.

url_to_postid

When page is top-level page, only slug has to be given.

e.g. url_to_postid('slug');

When the page is in lower hierarchy level (i.e. it has parent) you have to prepend parent slug divided by slash like so:

url_to_postid('parent-slug/child-slug');
<?php  function get_page_ID_by_slug( $slug ) {
    $page = get_page_by_path( $slug );
    if ( $page ) {
        return (int) $page->ID;
    }
    else {
        return null;
    }
}
?>

I hope this suggestion is helpful for someone.

Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top