I need help using pre_get_comments to limit comments in the comments admin screen

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;

});

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x