Get page content using slug

Use get_posts() and the parameter name which is the slug:

$page = get_posts([ 'name' => 'your-slug' ]);

if ( $page )
{
    echo $page[0]->post_content;
}

Be aware that the post type in get_posts() defaults to 'post'. If you want a page use …

$page = get_posts([
    'name'      => 'your-slug',
    'post_type' => 'page'
]);

If you want all public post types (except attachments) set the post type argument to 'any'. Then you could get more than one result because slugs are not unique across different post types.

Leave a Comment