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:
-
Comments made by the current user on any posts, and
-
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.