WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 20 down vote accepted

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

$page = get_posts( array( '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.

share|improve this answer

If on the page with the slug in question

Read up on conditional tags:
is_page() also takes the slug as an argument.


if( is_page( 'your-slug' ) ) {
     // fetch content

will do what you want.

If on another page

Should you be interested on how to fetch post/page content based on a slug when not on said page, you can feed get_posts a slug as well. This is not documented in the codex.

The following will fetch the id from a slug:

$args = array(
    'name' => 'your-slug'
$posts_from_slug = get_posts( $args );

// echo fetched content
echo $posts_from_slug[0]->post_content;
share|improve this answer

You can get a page by its title using get_page_by_title() function.

You can use it like this (assuming you want to show the content):

$page = get_page_by_title('Your Title'); 
$content = apply_filters('the_content', $page->post_content);
echo $content;

BTW, to Get page using slug:

function get_page_id_by_slug($slug){
    global $wpdb;
    $id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$slug."'AND post_type = 'page'");
    return $id;

$page = get_post(get_page_id_by_slug('my-slug'));
share|improve this answer

Use get_page_by_path.


<?php get_page_by_path( $page_path, $output, $post_type ); ?>



//Returns the animal with the slug 'cat'
get_page_by_path('cat', OBJECT, 'animal');

for more refernce see WordPress Function Reference

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.