SitePoint Sponsor

User Tag List

Results 1 to 1 of 1
  1. #1
    SitePoint Member
    Join Date
    Nov 2011
    0 Post(s)
    0 Thread(s)

    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 have a plugin that I'm trying to add a script that pulls comments based on a post_id

    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 
           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);
    Thanks for your help!
    Last edited by alxvallejo; Nov 10, 2011 at 13:50. Reason: Broaden the Question

Tags for this Thread


Posting Permissions

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