• Trinacy

    (@trinacy)


    I need a way to find out who commented during a specific time period. Is there a way to display this result?

    I’ve tried using;

    <?php
    $args = array(
    	'user_id' => 1, // use user_id
    
    );
    $comments = get_comments($args);
    foreach($comments as $comment) :
    	echo($comment->comment_author . '<br />' . $comment->comment_content);
    endforeach;

    But I can’t seem to get it to work for me.

Viewing 3 replies - 1 through 3 (of 3 total)
  • yorman

    (@yorman)

    It is not clear in your comment if you want to get all the comments (from a specific time period) for any post or page, so I will assume that that is what you want.

    In this case you have to query the table that holds the data of the comments and filter the results by the column “comment_date” or “comment_date_gmt”, WordPress allows you to use the argument “date_query” [1] to facilitate this job. If you also want to get the comments from a specific post you have to add the filter “post_id” with the identifier of the post or page.

    As stated by the official documentation of the “get_comments” function [2] you could use something like this (first block) to accomplish that result. But I personally would execute a raw query using the “WP_Query” class [3] it will give you more control over the filtering, or even the “get_results” method from the global variable “$wpdb” may be easier (second block).

    # First Block
    $comments = get_comments( array(
        'date_query' => array(
            'after' => '4 week ago',
            'before' => 'tomorrow',
            'inclusive' => true,
        )
    ) );
    var_dump( $comments );
    
    # Second Block
    global $wpdb;
    $date_time = '2015-06-02 15:20:00';
    $query = $wpdb->prepare(
        "SELECT * FROM {$wpdb->comments}
        WHERE comment_date >= %s
        ORDER BY comment_ID ASC",
        $date_time
    );
    $comments = $wpdb->get_results( $query );
    var_dump( $comments );
    

    [1] https://developer.ww.wp.xz.cn/reference/classes/wp_date_query/
    [2] https://codex.ww.wp.xz.cn/Function_Reference/get_comments
    [3] https://codex.ww.wp.xz.cn/Class_Reference/WP_Query

    Thread Starter Trinacy

    (@trinacy)

    My apologize. I want to be able to see who has commented on any posts throughout the site. So basically I’d like to be able to see a list of every registered account that has commented on my website this week, last week etc.

    yorman

    (@yorman)

    Okay, I do not know how to do that using the built-in WordPress functions (they have their own limits), but you can get the results that you want using this SQL query:

    SELECT wp_comments.comment_ID, wp_comments.comment_date,
    wp_comments.comment_content, wp_users.ID, wp_users.user_login,
    wp_users.user_nicename, wp_users.display_name, wp_users.user_registered
    FROM wp_comments LEFT JOIN wp_users ON wp_comments.user_id = wp_users.ID
    WHERE wp_comments.comment_approved = 1
    AND wp_comments.comment_date >= '2015-06-01'
    ORDER BY wp_comments.comment_ID;

    This is basically making a left join between the comment and user tables where the query associates each comment with an user account if the entry was created by a registered user, if the “comment_ID” comes with a NULL value it means that the entry was created by a non-registered user. Then the query filters the results to get only the approved comments, and the ones that were created after a specific date (2015-06-01 in this example).

    Someone else may help you to integrate this SQL with the built-in WordPress functions, but if you feel lucky you can just use this code like is and it must work (I already tested).

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Find out who commented’ is closed to new replies.