I am trying to get all the comments of the logged in user in the "foods" CPT but I'm still with no success.

Here's my code:

$args = array(
        'user_id' => $user->ID,
        'post_type' => 'foods', // when I removed this, the list only showed the comments left on posts but not on Foods CPT

    $comments = get_comments( $args );

    if ( $comments )
        $output.= '<ul>';
        foreach ( $comments as $c )
        $output.= '<li>';
        $output.= '<a href="'.get_comment_link( $c->comment_ID ).'">';
        $output.= get_the_title($c->comment_post_ID);
        $output.= '</a>, Posted on: '. mysql2date('m/d/Y', $c->comment_date, $translate);
        $output.= "</li>\n";
        $output.= '</ul>';

        echo $output;

    } else { echo "<p>No comments yet.</p>";}

This code returns no comments despite me, the user, commenting on posts in Foods CPT. Plus, when I deleted the post_type line, instead of showing all the comments I made, it only showed the comments I left on Posts and not on Foods CPT.

I created the CPT through WP Types but to ensure that it's not the plugin that's making the problem, I manually created another CPT in the functions file and commented on the post I created there. Still, my comment in this CPT did not show.

What could be the problem behind this?

  • How you created the Foods CPT? Can you share the CPT code? – Chinmoy Kumar Paul Sep 7 '14 at 20:09
  • I created it using the WP Types plugin. – Bernie Sep 8 '14 at 1:33
  • are you added 'comments' in support section? Are you getting the discussion section below the WP Editor at dashboard? – Chinmoy Kumar Paul Sep 8 '14 at 3:30
  • Yes, it appears in the Comments section of the Dashboard. – Bernie Sep 8 '14 at 6:27
  • Because the post has no comment? – kdgilang Aug 22 '16 at 8:53

For some reason, 'user_id' => $user->ID, is not working since I did not set global current_user on. The first three lines should be replaced with something like this:

$args = array(
        'user_id' => get_current_user_id(),

Now it's working :)

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.