0

I have a template to get the comment replies to a user and display it on a static page. It works reasonably well in so far as the correct comments are displayed but whatever I try I can't get the replies to order by comment date, the newest first, but instead they will order by their parent comment first and after that by date. Also, the pagination is not displaying. I would be most grateful for any help or hints here!

<?php 
global $current_user;
get_currentuserinfo();
define('DEFAULT_COMMENTS_PER_PAGE',6);
$page = (get_query_var('page')) ? get_query_var('page') : 1;
$limit = DEFAULT_COMMENTS_PER_PAGE;
$offset = ($page * $limit) - $limit;
$param = array(
    'status'=>'approve',
    'user_id' => $current_user->ID,
    'comment_parent' => 0
);
$parent_comments = get_comments($param); ?>
<?php foreach($parent_comments as $parent_comment) : ?>
<?php $parent_comment_id = $parent_comment->comment_ID;
$comment_args = array(
    'orderby' => 'comment_date',
    'status' => 'approve',
    'parent' => (int)$parent_comment_id,
    'offset' => $offset,
    'number' => $limit
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $comment_args );
if ( $comments ) : ?>
    <?php foreach($comments as $reply) : ?>
        <div class="comment-body">
        <?php echo $reply->comment_author; ?>has left a
            <a href="<?php echo get_comment_link( $reply->comment_ID ); ?>">reply</a> to your <a href="<?php echo get_comment_link( $parent_comment->comment_ID ); ?>">comment</a>
            <div class="comment-content">
            <?php echo apply_filters('the_content', $reply->comment_content); ?>
            </div>
        </div>
    <?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>

<div id="pagination">

<?php $pages = ceil(count($comments)/DEFAULT_COMMENTS_PER_PAGE); ?>

<?php
$args = array(
    'base' => 'http://mywordpress.example/user/replies/%#%',
    'format' => '?page=%#%',
    'total' => $pages,
    'current' => $page,
    'show_all' => False,
    'end_size' => 1,
    'mid_size' => 2,
    'prev_next' => True,
    'prev_text' => __('Previous'),
    'next_text' => __('Next'),
    'type' => 'plain');

 echo paginate_links( $args );
?>
  • Sorry, it looks to me like your own code is going to produce the effect you don't want, as you retrieve parent comments first and then query for children. – s_ha_dum Jan 5 '14 at 22:57
  • Thanks, but how can we then at all be able to query for replies to the current user if we do not first query for parent comments to find those children that are replies to the current users comments? – psot Jan 5 '14 at 23:57
  • I think you need to very clearly, very carefully explain what you are trying to achieve. – s_ha_dum Jan 6 '14 at 0:04
  • Every user who is logged in has a front-end page where he can see all comments which are replies to comments the user has made himself. The replies the user himself has made to his own comments are also included. These comments are successfully collected inside $comments thanks to the 'parent' => (int)$parent_comment_id argument but this also results in the iteration of $comments ordering the comment replies after the ID of the parent comment. I somehow need to rearrange $comments or alter the sorting of the last foreach loop. I thought it should be quite trivial, but maybe I am wrong. – psot Jan 6 '14 at 0:43
  • correction of the last comment: "the iteration of $comments sorting* the comment replies..." – psot Jan 6 '14 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.