I found this code on stackoverflow, but how do I output the actual content rather than just that ID line in the example!

        $the_slug = 'my-page';
        'name' => $the_slug,
        'post_type' => 'page',
        'post_status' => 'publish',
        'numberposts' => 1
        $my_posts = get_posts($args);
        if( $my_posts ) {
            echo 'ID on the first post found '.$my_posts[0]->ID; the_content();
  • 1
    Do a print_r($my_posts). There you will find what you need – ifdion Jan 21 '15 at 11:23

This is a "custom" loop outside of the main WordPress query (query_posts), you will have to tell WordPress to setup the post data using setup_postdata()

More info on get_posts() is found here, giving you basically what I am about to write below: http://codex.wordpress.org/Template_Tags/get_posts

Tip: The WordPress Codex is the best friend you will ever have, apart from Google.


$the_slug = 'my-page';

$args = array(
    'name'          => $the_slug,
    'post_type'     => 'page',
    'post_status'   => 'publish',
    'numberposts'   => 1

$my_posts = get_posts($args);

if( $my_posts ) {

    echo 'ID on the first post found '.$my_posts[0]->ID;

    // To get the content of the first post:
    echo apply_filters('the_content', $my_posts[0]->post_content);

    // if you now wanted to remove the first post from this loop and assign it to a different variable $first_post
    // However, it looks as if you are only grabbing one "post" being a "page" from the slug "my-page"
    $first_post = $my_posts[0];

    foreach($my_posts as $p): setup_postdata($p); 

        // Now you can use the_title(), the_content() etc as you normally would



// Reset WordPress Loop & WP_Query

  • The main query is using WP_Query – Pieter Goosen Jan 21 '15 at 12:31


for proper look you'll need to apply filter the_content and do some replace ( like the_content() does )

str_replace( ']]>', ']]&gt;', apply_filters('the_content', $my_posts[0]->post_content ) );

if your going to use it a lot i suggest u make your own function:

function my_get_content( $content ) { return str_replace( ']]>', ']]&gt;', apply_filters('the_content', $content ) );

and then call it like so: echo my_get_content( $my_posts[0]->post_content );

  • Thanks for the answer. As the chap above demonstrated, you can actually do it with this code: echo apply_filters('the_content', $my_posts[0]->post_content); – wilsonf1 Jan 22 '15 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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