0

I am trying to use get_post_meta to return a value from the post meta, it was working completely fine then suddenly stopped working. I've tried and checked everything and i cannot seem to get it working again - the code i'm using is as below - no matter what i do, i cannot return the meta value for the meta field any more.

The code is added to functions.php. It literally just stopped working, it was previously pulling in the correct data now i cannot pull in any meta data whatsoever.

function latest()   {
$args = array( 'posts_per_page' => 1);                  
$last_5_posts_query = new WP_Query( $args );
while($last_5_posts_query->have_posts()) : 
    $last_5_posts_query->the_post();
    $link = get_permalink();
    $title = get_the_title();  
    $title1 = get_post_meta(get_the_ID(), 'Title 1', true);     
    $title2 = get_post_meta(get_the_ID(), 'Title 2', true);  
    $more = 'Read more...';    

    $content .= '<div class="top_titles">';
    $content .= '<h3><a href="#top_titles">'  .$title1.  '</a></h3>';
    $content .= '<h3><a href="#top_titles">'  .$title2.  '</a></h3>';
    $content .= '</div>';
endwhile;

return $content;
}

Any ideas? The meat names "Title 1" and "Title 2" still exist. Completely out of ideas.

  • 1
    Is that the real key "Title 1"? Did you try with "Title_1"? – JMau Jun 24 '13 at 18:50
  • Yeah, the key is "Title 1" exactly, i tried "Title_1" for debugging and still the same. – Nikki Mather Jun 24 '13 at 18:59
  • 1
    I guess there's some kinda of bad interaction because the code seems fine, by curiosity why do you keep $link = get_permalink(); while not using $link? – JMau Jun 24 '13 at 19:05
  • Okay the issue is related to the $args = array( 'posts_per_page' => 1); - whenever this is set to 1, the post_meta doesn't show, when set to 2, the first title shows but i cannot get the second title to show now... Edit: I will be using $link eventually – Nikki Mather Jun 24 '13 at 19:06
  • 1
    You probably added a new post, without Title 1 and Title 2 set, and that's why "it's not working". Check that data actually exists for it before debugging further. – TheDeadMedic Jun 25 '13 at 1:01

Your Answer

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

Browse other questions tagged or ask your own question.