0

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

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

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

<?php

$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>';
3
  • 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 – Nicola Bertelloni Mar 1 '16 at 8:18
  • But you do know the post ID, it's in your original code. – TheDeadMedic Mar 1 '16 at 10:27
  • The post ID gets sent in the AJAX request when a user clicks, no? – TheDeadMedic Mar 1 '16 at 10:28

Your Answer

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

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