WordPress Retrieve Post by Title

The following code works brilliantly for retrieving certain pages, but I can’t get it to retrieve POSTS by name. I’d like exactly the same code, but to retrieve posts rather than pages. Any help appreciated.

<?php
    function get_my_content($page) {
        $my_id = $page;
        $post_id = get_post($my_id, ARRAY_A);
        $content = $post_id['post_content'];

        return $content;
    }
?>    

<div id='test'>
    <?php 
        $page = get_page_by_title('testpage');
        echo get_my_content($page);
    ?>
</div>          

Related posts

Leave a Reply

3 comments

  1. Using that earlier question I was referring to, you could do something like this:

    <?php
      function get_my_content($page_title) {
        global $wpdb;
        $post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type='post'", $page_title ));
        if ( $post )
          $post_id = get_post($my_id, ARRAY_A);
          return $post_id['post_content'];
    
        return null;
      }
    ?>
    
    <div id='test'>
        <?php 
            $page = get_page_by_title('testpost');
            echo get_my_content($page);
        ?>
    </div>