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

  $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>';

at the moment i have this piece of code but i think is basically entirely wrong

The page where i need to laod this is http://www.nicolabertelloni.it/homus-wp/ in the box over the footer. you can see the entire code at https://github.com/wanbinkimoon/homus-web.git the code i'm referring is in homus-web/wp-content/themes/homus-theme/single_pb_post_details.php

1 Answer 1


You don't need a query if you the know the post ID:


$post_id = absint( $_POST['post_id'] );

if ( ! $post = get_post( $post_id ) )
    exit; // Error

if ( $post->post_status !== 'publish' )
    exit; // Might want to prevent script kiddies from accessing private content

echo '<h4>' . get_the_title( $post ) . '</h4>';
  • my problem is with the query becuse i don't know the post id, the post id change based on the click of the user Commented Mar 1, 2016 at 8:18
  • But you do know the post ID, it's in your original code. Commented Mar 1, 2016 at 10:27
  • The post ID gets sent in the AJAX request when a user clicks, no? Commented Mar 1, 2016 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.