1

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>          
8
  • Where and how is "get_my_content" defined? Sep 25, 2011 at 17:50
  • I have no idea I'm afraid. I'm new to php, someone else gave me that piece of code, all I know is that the code above is all that's required to pull in multiple pages to different divs. But for some reason it only works with pages, not posts.
    – SparrwHawk
    Sep 25, 2011 at 17:56
  • I'm sorry my bad, the definition is in your example. However this should return posts already, if you use a posts id instead of a pages. Have a look here: stackoverflow.com/questions/1536682/… Sep 25, 2011 at 18:04
  • I think it's something to do with get_page_by_title, because by default it's page, however there are no examples of how to set it to 'post' codex.wordpress.org/Function_Reference/get_page_by_title
    – SparrwHawk
    Sep 25, 2011 at 18:04
  • Yes I saw that question, I'd rather just adapt my code though. Think I just need to tweak the $page = get_page_by_title bit to set it to post type.
    – SparrwHawk
    Sep 25, 2011 at 18:06

3 Answers 3

1

As per docs you only need to specify different post type to make it work:

$page = get_page_by_title('testpage', OBJECT, 'post');
1
  • Great, that worked! I knew it was something simple. Thanks v much Rarst
    – SparrwHawk
    Oct 15, 2011 at 17:29
0

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>
3
  • Thanks Jasper, I tried this but it didn't work. Think it's also missing a closing } and $page should be $post, but still didn't work.
    – SparrwHawk
    Sep 25, 2011 at 18:38
  • A closing bracket was missing indeed, I've edited that. The name of the variable doesn't matter, and it's good this way I believe, although it's hard coding without seeing the results. Are you sure you're searching for an existing title? Because if the title doesn't exist, nothing is returned, obviously. Sep 25, 2011 at 19:32
  • Hi Jasper, tried again but no result unfortunately. The post definitely exists.
    – SparrwHawk
    Sep 26, 2011 at 8:45
0

Does this post over on Wordpress.org help you? http://wordpress.org/support/topic/get-posts-based-on-page-title

1
  • Thanks Chrs, interesting information but it didn't really help me fetch a post by title, like the above code does with pages.
    – SparrwHawk
    Sep 26, 2011 at 8:43

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.