I’m trying to get page content when I only know the slug string.
Is there a function for this, or an easy way to do this or is this a case of doing it via SQL?
Thanks very much
I’m trying to get page content when I only know the slug string.
Is there a function for this, or an easy way to do this or is this a case of doing it via SQL?
Thanks very much
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.