Support » Fixing WordPress » retrieve post title based on a known post id

  • Resolved pfv



    i am kind of new to wp and i have been having some problems trying to find a wp function or a way to hack wp in order to retrieve a post title based solely on a post id. is this even possible?

    both functions the_title() and get_the_title() give me the title of the current post, which is not what i need. there are functions both to retrieve the category and post meta based on a given id, but i can’t find a way to do the same with the title. am i missing something?

    here is the code i have. so far it displays an image at the top of the page retrieved from a link provided in a custom field from the most recent post that contains another custom field stating that the image related to this post can be shown in the header (custom field ‘show_img’ with the value ‘true’). except for the title it does everything i need at this point…

      'meta_key' => 'show_img',
      'meta_value' => 'true',
      'posts_per_page' => 1,
      'caller_get_posts'=> 1
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      if ($my_query->posts[0]->post_status == 'publish' ) {
        $p_id_img = $my_query->posts[0]->ID;
        //echo $p_id_img;
        $category = get_the_category($p_id_img);
    echo '<div id="top_txt"><cat>' . $category[0]->cat_name . '</cat>';
    $data_r = get_post_meta($p_id_img, 'data_real', true);
    echo '<dat>' . $data_r . '</dat>';
    the_title('<tit>', '</tit></div>');
    $top_image = get_post_meta($p_id_img, 'agenda', true);
    echo '<img src="' . $top_image . '" width="630" />';

    this was the only way i found not to mess with the loop. since this code is written in my header.php file the slightest variation can cause all sorts of problems with the rest of the blog. i’ve tested many possibilities and this one almost gets to what i need…

    can anyone help?

    thanks in advance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • For outside of the loop
    replace ID with the post id number

    $post_id = get_the_title(ID);

    thanks. i thought i had tested it, but apparently i had not. i may have not waited long enough before refreshing the page making it seem like the error persisted… i don’t know…

    thank you very much.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘retrieve post title based on a known post id’ is closed to new replies.