Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>          
share|improve this question

migrated from stackoverflow.com Sep 26 '11 at 6:57

This question came from our site for professional and enthusiast programmers.

    
Where and how is "get_my_content" defined? –  Jasper Kennis Sep 25 '11 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 '11 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/… –  Jasper Kennis Sep 25 '11 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 '11 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 '11 at 18:06

3 Answers 3

up vote 1 down vote accepted

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

$page = get_page_by_title('testpage', OBJECT, 'post');
share|improve this answer
    
Great, that worked! I knew it was something simple. Thanks v much Rarst –  SparrwHawk Oct 15 '11 at 17:29

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>
share|improve this answer
    
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 '11 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. –  Jasper Kennis Sep 25 '11 at 19:32
    
Hi Jasper, tried again but no result unfortunately. The post definitely exists. –  SparrwHawk Sep 26 '11 at 8:45

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

share|improve this answer
    
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 '11 at 8:43

Your Answer

 
discard

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.