• Resolved topsys

    (@topsys)


    Hi,

    I am having a problem with comments on my posts. It works like normally when I post a comment to a post, but if I add an attachment to the same post, the comments disappear. If I now add a comment, once submitted, it goes to the first attachments page upon submit. If I then go back to the post page, the new comments are there but not the original comments, they will only appear again, once I delete the attachment.

    I have tried to search the forum and google, but I guess I am not using the right search term, as I have not found any thing on this issue.

    Anybody have an idea on how to get the comments to attach to the original post and not the post attachments?

    Thanks,
    olaf

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Are you using a plugin to add attachments?

    Thread Starter topsys

    (@topsys)

    Hi Ipstenu,

    thanks for your reply.

    No, I just upload them inside a post and they automatically get attached to that post, then I use the get_children function to get the attachments for the specific post.

    /olaf

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    At a guess, it’s the get_children function, somehow overriding the page’s normal comment functionality.

    Can you share the code you used to implement get_children?

    Thread Starter topsys

    (@topsys)

    sorry about the late reply, here are the two functions I have tried, both show a list of the attachments.
    The first one using get_children:

    $args = array(
      'post_type' => 'attachment',
      'numberposts' => -1,
      'post_status' => 'inherit',
      'post_parent' => get_the_ID(), // any parent
    );
    $attachments = get_children($args);
    if ($attachments) {
      echo "<ul>\n";
      foreach ($attachments as $post) {
        setup_postdata($post);
        echo '<li><a href="' . wp_get_attachment_url($post->ID) . '">'. get_the_title().'</a></li>'."\n";
      }

    and the second one using get_posts:

    $args = array(
      'post_type' => 'attachment',
      'numberposts' => -1,
      'post_status' => 'inherit',
      'post_parent' => get_the_ID(), // any parent
    );
    $attachments = get_posts($args);
    if ($attachments) {
      echo "<ul>\n";
      foreach ($attachments as $post) {
        setup_postdata($post);
        echo '<li><a href="' . wp_get_attachment_url($post->ID) . '">'. get_the_title().'</a></li>'."\n";
      }

    Both the get_children and the get_posts above are pretty much like the ones on the get_childrens function page and the get_posts function page on how to get attachments, so it should not be here that the problem occurs.

    /olaf

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Okay. So you’re using children to auto-generate a list of attachments, versus manually attaching them to your posts. Repeating to clear my own head ??

    All I can find related to this is this: https://digwp.com/2009/08/awesome-image-attachment-recipes-for-wordpress/

    Which just explains how one dynamically generates such things, but seems to be image specific.

    Thread Starter topsys

    (@topsys)

    I figured out a way that works, now, while using the method below, I have my list of attachments for the post and am still able to post a comment to the post and not the attachment.

    Here is the database query code:

    global $wpdb;
    $attachment_ids = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC"); 
    
    foreach($attachment_ids as $aid) {
      echo '<li><a href="' . wp_get_attachment_url($aid->ID) . '">'. get_the_title($aid->ID) .'</a></li>'."\n";
    }

    My guess is that using the other database queries somehow changed the post id number which is passed when submitting a comment, thereby changing the location of the comment, even though it would still show up in the post, it was attached to the posts first attachment.

    Anyways, I hope this helps somebody else in the future.

    Thanks for all your help Ipstenu.

    /olaf

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Comments get attached to post attachment’ is closed to new replies.