Results 1 to 2 of 2

Thread: Get Comments From Post ID -- SQL method help!

  1. #1
    alxvallejo is offline Hello World
    Join Date
    Nov 2011
    Location
    Boston, MA
    Posts
    4

    Default Get Comments From Post ID -- SQL method help!

    I'll split my question in two parts: First; Am I going about this the right way and Second; See the SQL string below (Any feedback is welcome)

    Basically, I have a plugin that pulls content from a post based on a slug wrapped in [[post-title]]. You can then go to a page, insert this shortcode-hybrid syntax directly into the text field, and the page will spit out the content from the selected post ID.


    I need to attach the comments from that post to the bottom of the content. I figure there's two ways to attack the problem:


    A) use the template_tags
    use the $wpdb


    I'm using the latter method and I try placing a variable in the SQL string:


    WHERE comment_approved = '1' AND comment_post_id='" . $post_id . '"


    Unfortunately this does not work.




    I read somewhere that this script would work but it does not:

    PHP Code:
    $sql "
             SELECT DISTINCT comment_post_ID, comment_author, comment_date_gmt, comment_approved, SUBSTRING(comment_content,1,100) AS com_excerpt
             FROM $wpdb->comments
             WHERE comment_approved = '1' AND comment_post_id = %s
             ORDER BY comment_date_gmt DESC
             LIMIT 5"
    , ($post); 
    I get a fatal error: unexpected ',' on that last line...
    $post is defined in another function (in my plugin):

    PHP Code:
    $post $wpdb->get_var$wpdb->prepare"SELECT ID FROM $wpdb->posts WHERE post_name = %s"$post_name ));
          if ( 
    $post ) return get_post($post$output);
          return 
    null;
          } 
    Thanks for your help!
    Alex
    Last edited by alxvallejo; 11-10-2011 at 01:49 PM.

  2. #2
    StephenCronin's Avatar
    StephenCronin is offline Hello World
    Join Date
    Feb 2009
    Location
    Brisbane
    Posts
    11

    Default

    Hi alxvallejo,

    It could be many things, but looking closely at the following line:

    Code:
    WHERE comment_approved = '1' AND comment_post_id='" . $post_id . '"
    it may just be a matter of having the quotes in the wrong order. I've added spaces to code to highlight this - you seem to have:

    Code:
    ' " . $post_id . ' "
    where you should have:

    Code:
    ' " . $post_id . " '
    Note the order of the last couple of characters. I'd try that first...

    Cheers,
    Stephen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •