0

I am trying to limit the comments in the comments admin screen to:

  1. Display all comments for the current user
  2. Display all the comments made on the current user's post

if(!current_user_can('edit_others_posts')) {
    
    add_action('pre_get_comments', function($wp_query) {

        global $user_ID;

        // all current user comments
        $wp_query->set( 'user_id', $user_ID );

        // FIXME: condition to get comments made on current user's post
        
    });
}

I am facing two problems:

  1. The code generates comments for all users and seems to ignore $wp_query->set( 'user_id', $user_ID )
  2. I can't figure out a condition for the FIXME statement

Any help is appreciated! Thank you

Update

Unfortunately, 'post__in', 'user_is', and similar arguements limit the query instead expanding it.

As a result, I used a very long appraoch to get what the results that I want. However, I am waiting for an improved version of my appraoch before I can accept an answer.

Here's what I revised:

add_action('pre_get_comments', function($wp_comment_query) {

    global $wpdb;
    $user_ID = get_current_user_id();
    
    $post_ids = "
        SELECT $wpdb->posts.ID 
        FROM $wpdb->posts
        WHERE $wpdb->posts.post_author = $user_ID 
    ";
    $post_ids = $wpdb->get_results($post_ids, ARRAY_N);
    $post_ids = array_column($post_ids, '0');
    $post_ids = implode(',', $post_ids);

    $comment_ids = "
        SELECT $wpdb->comments.comment_ID 
        FROM $wpdb->comments
        WHERE $wpdb->comments.user_id = $user_ID
        OR $wpdb->comments.comment_post_ID IN ($post_ids)
    ";
    
    $comment_ids = $wpdb->get_results($comment_ids, ARRAY_N);
    $comment_ids = array_column($comment_ids, '0');
    
    $wp_comment_query->query_vars['comment__in'] = $comment_ids;

});
1
  • The update that I provided takes input from @Sally and it works, but I think that it can be refactored and that is why I will not post my update as an answer Jul 3 at 14:37
1

Update

In response to the new code in the updated question, you could actually use $wpdb->get_col() with a single SQL command via a JOIN clause, like so:

$query = "SELECT c.comment_ID FROM $wpdb->comments c
    JOIN $wpdb->posts p ON p.ID = c.comment_post_ID
WHERE c.user_id = $user_ID
    OR p.post_author = $user_ID";

$comment_ids = $wpdb->get_col( $query );

However, instead of running that manually via the pre_get_comments hook, you could actually use the comments_clauses hook to add the above JOIN clause and WHERE conditions to the SQL command generated by WP_Comment_Query.

add_filter( 'comments_clauses', function ( $pieces ) {
    global $wpdb;

    $user_ID = get_current_user_id();

    if ( false === strpos( $pieces['join'], "JOIN $wpdb->posts ON" ) ) {
        $pieces['join'] .= " JOIN $wpdb->posts ON {$wpdb->posts}.ID = comment_post_ID";
    }

    $pieces['where'] .= " AND (user_id = $user_ID OR {$wpdb->posts}.post_author = $user_ID)";

    return $pieces;
} );

So try that, which should return:

  1. Comments made by the current user on any posts, and

  2. Comments by other users, but only on the posts where the current user is the author.

Original Answer

The first (and only) parameter from the pre_get_comments hook is actually an instance of the WP_Comment_Query class and not WP_Query, so the $wp_query->set() won't work because WP_Comment_Query does not have the set() method.

But you can directly access the $query_vars property in WP_Comment_Query to change the value of query args like the user_id. So for example with that arg, you would use $wp_query->query_vars['user_id'] = $user_ID;.

As for the FIXME part, try with the post_author arg: $wp_query->query_vars['post_author'] = $user_ID; which should work if you're indeed trying to display only the comments made by the current user on the posts where that user is the author.

PS: I would rename $wp_query to $query or maybe $wp_comment_query.. I would also use $user_ID = get_current_user_id(); instead of relying upon the $user_ID global.

8
  • $wp_query->query_vars['post_author'] = $user_ID restricts the comments to only posts made by the current user. If only there was a way to add this argument as an "OR" statement in the query instead of and "AND" Jul 3 at 12:53
  • I think that i should find a way to use post__in instead of post_author Jul 3 at 13:05
  • According to WordPress: post__in = Array of post IDs to include affiliated comments for. It is actually returning zero comments and when pass an array of current user posts ids to post__in? What gives Jul 3 at 13:29
  • Actually, with the original code in the question, have you already tried replacing the $wp_query->set( 'user_id', $user_ID ) with $wp_query->query_vars['user_id'] = $user_ID; $wp_query->query_vars['post_author'] = $user_ID; ? What was the code you tried with? And if I'm not mistaken, the new code in your updated question is basically leading to the same outcome.. 🤔
    – Sally CJ
    Jul 3 at 19:10
  • Yes, and it restricted all comments to $user_ID Jul 3 at 22:18

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.