0

I want to add a page template inside my custom post type and retrieve inside that page the comments posted by current user.

I have this code for getting the current user:

<!-- GET CURRENT USER -->

<?php

global $current_user;
get_currentuserinfo();

echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
echo 'User ID: ' . $current_user->ID;
?>

The code for getting the comments of the current user:

<!-- GET COMMENTS OF USER -->
<?php 
$args = array(
'user_id' => 1, // use user_id
    'post_type' => 'my-CPT',
 );
$comments = get_comments($args);
foreach($comments as $comment) :
    echo($comment->comment_author . '<br />' . $comment->comment_content);
endforeach;

?>

And the code for listing the comments:

<ol class="commentlist">
            <?php
                wp_list_comments( array( 'callback' => 'outbox_comment' ) );
            ?>
</ol><!-- .commentlist -->

And this is how my page-commentsbyuser.php is looking:

<?php

/**
 * The template for displaying a list of comment from the current user
 *
 */

get_header();

    ?>
<div id="primary" class="row">

    <div id="content" class="span9" role="main">

<!-- GET CURRENT USER -->   
<?php
global $current_user;
get_currentuserinfo();
echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
echo 'User ID: ' . $current_user->ID;
?>
    <!-- GET COMMENTS OF USER -->

<?php 
$args = array(
'user_id' => 1, // use user_id
    'post_type' => 'my-CPT',
 );
$comments = get_comments($args);
foreach($comments as $comment) :
    echo($comment->comment_author . '<br />' . $comment->comment_content);
endforeach;

?>
    <ol class="commentlist">
            <?php
                wp_list_comments( array( 'callback' => 'outbox_comment' ) );
            ?>
        </ol><!-- .commentlist -->
    </div><!-- #content .site-content -->

</div><!-- #primary .content-area -->

This is not working and also I don't know how can I get the comments of the current user.

Any suggestions on how I can retrieve the comments from the CPT of the current user inside this page template ?

2

This is not working and also I don't know how can I get the comments of the current user.

You have already used the current user's ID in your code...

echo 'User ID: ' . $current_user->ID;

... but you hard-code a single ID in the get_comments argument array. Don't do that. Use the current user ID.

$args = array(
    'user_id' => $current_user->ID, // use user_id
    'post_type' => 'my-CPT',
);

I am uncertain of the relationship between your user comments and wp_list_comments. You have already output the user comments so I don't why you are running wp_list_comments unless you are trying to output a different set of comment or you are trying to output the same comments again (which is weird). Either way, you need to pass wp_list_comments a set of comments as in this example from the Codex (re-formatted to be less confusing):

//Gather comments for a specific page/post 
$comments = get_comments(
  array(
    'post_id' => XXX,
    'status' => 'approve' //Change this to the type of comments to be displayed
  )
);

//Display the list of comments
wp_list_comments(
  array(
    'per_page' => 10, //Allow comment pagination
    'reverse_top_level' => false //Show the latest comments at the top of the list
  ), 
  $comments
);

Note the second argument $comments, which is the output of a call to get_comments. The context you seem to be using this in makes me think that that is going to be required.

  • Now is working ! I've noticed that ` <ol class="commentlist">` is empty also, is this supposed to be empty or is something wrong? – agis Nov 28 '13 at 20:01
  • @Alecs : see the edit – s_ha_dum Nov 28 '13 at 21:59

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.