Show WordPress comments to the post author only

In a new WordPress template, I want to customize single.php in order to show the list of comments to the post author only.

Other users (guests and other post authors) should see no comments or blank space.

Read More

In summary, the post author (when logged) can see the list of comments in the bottom of his posts, but nothing in the bottom of posts written by other authors.

What conditional code should I apply to the following code?

<?php 
global $wpdb,$current_user;
$limit = 10; //this is limit per day per user
$comment_count = $wpdb->get_var( $wpdb->prepare("
SELECT count(*)
FROM cxp_comments
WHERE comment_author = '%s'
AND comment_date >= DATE_SUB(NOW(),INTERVAL 1 DAY);"
,$current_user->user_login) );

if($comment_count < $limit): ?>

<h2 class="comments-title"> Want to get and review this product?</h2>
<form action="http://localhost/reviews/wp-comments-post.php" method="post"     id="commentform" class="comment-form" novalidate="">
<p class="comment-amaz" style="display:none;"><input id="user-rank"     name="user-rank" aria-required="false" class="" type="text" value="<?php echo     do_shortcode('[mycred_my_ranking]'); ?>"></p>
<p class="comment-amazi" style="display:none;"><input id="user-points"     name="user-points" aria-required="false" class="" type="text" value="<?php echo     do_shortcode( '[mycred_my_balance wrapper="0" title_el="" balance_el=""]' ); ?>">    </p>
<p class="comment-form-comment">
<textarea id="comment" name="comment" placeholder="Insert your Amazon Public     Profile Link" aria-required="true"></textarea></p>
<p class="form-submit"><input name="submit" id="submit" class="submit"     value="Review this product!" type="submit"> <input name="comment_post_ID" value="    <?php global $post;
echo $post->ID; ?>" id="comment_post_ID" type="hidden">
<input name="comment_parent" id="comment_parent" value="0" type="hidden">
</p> </form>
<?php endif; ?>


<?php if($comment_count > $limit): ?>
<p>Exceeded comments limit for today. Please, come back tomorrow!</p>

<?php endif; ?>

This is made for the frontend only. I don’t care about admin panel.

Related posts

Leave a Reply

1 comment

  1. Check if the user is logged in with is_user_logged_in():

    if(is_user_logged_in())
    

    Then get the User login with wp_get_current_user():

    $curUser = wp_get_current_user();
    $curLogin = $curUser->user_login;
    

    Then get the Author login with get_the_author_meta():

    $autLogin = get_the_author_meta('user_login');
    

    Finally, compare:

    if($autLogin == $curLogin) wp_list_comments();
    

    Full code:

    if(is_user_logged_in()){
        $curUser = wp_get_current_user();
        if(get_the_author_meta('user_login') == $curUser->user_login) wp_list_comments();
    }