Nicola Bertelloni
Nicola Bertelloni

Reputation: 333

WP_Query load the selected post

I need to load a selected post in a ajax function, I have some problems writing the appropriate query that calls the post i'm pointing

<?php
 $pb_id = $_POST['post_id'];
 $pb_details_args = array(
   'p' => $pb_id,
  );

 $pb_details_query = new WP_Query( $pb_details_args );

while ($pb_details_query -> have_post() ):
  $pb_details_query -> the_post();
  echo '<h4>' . get_the_title() . '</h4>';
endwhile
?>

at the moment i have this piece of code but i think is basically entirely wrong, can you help me?

Upvotes: 1

Views: 33

Answers (1)

Goran Jakovljevic
Goran Jakovljevic

Reputation: 2820

If you need just title, you can go with:

<?php
echo get_the_title($_POST['post_id']);
?>

that will give you jsut post title.If you need everything else:

<?php
$single_post = get_post( $_POST['post_id'] ); 
echo $single_post->post_title; // title
?>

Full reference:

WP_Post Object
(
    [ID] =>
    [post_author] =>
    [post_date] => 
    [post_date_gmt] => 
    [post_content] => 
    [post_title] => 
    [post_excerpt] => 
    [post_status] =>
    [comment_status] =>
    [ping_status] => 
    [post_password] => 
    [post_name] =>
    [to_ping] => 
    [pinged] => 
    [post_modified] => 
    [post_modified_gmt] =>
    [post_content_filtered] => 
    [post_parent] => 
    [guid] => 
    [menu_order] =>
    [post_type] =>
    [post_mime_type] => 
    [comment_count] =>
    [filter] =>
)

Upvotes: 1

Related Questions