Skip Navigation

[Resolved] Building a view that shows most recently commented posts

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user wanted to display the last commented posts.

Solution:
As you can see in the WordPress database structure, comments are stored in another table than the one for posts. Currently, Toolset does not query/create/update/delete comments. It only acts on the posts and taxonomies tables. https://codex.wordpress.org/Database_Description
There are no shortcodes that will allow you to pull the comments data, either directly, or from posts on a view's query.

I actually did not test the solution, I have suggested on that ticket. You may be right, and "comment_post" is probably not the best hook to use. "transition_comment_status" seems to be a good candidate. This pseudo-code may help:


// update post last comment field when a comment is approved
function update_last_comment_field( $new_status, $old_status, $comment ) {
    // Only when the status is approved.
    if( 1 === $new_status ){
        // get the post ID
        $post_id = $comment->comment_post_ID ;
         
        // get the post object
        $post = get_post( $post_id );
         
        // only for post type "post"
        if ( $post->post_ype == "post" ) {
            // update custom field. Toolset field slug is prefixed with "wpcf-"
            update_post_meta( $post_id, 'wpcf-last-comment-time', time() );
        }
    }
}

// Execute this action when a comment is saved
add_action( 'transition_comment_status', 'update_last_comment_field', 10, 3 );
I might also suggest creating a custom shortcode that will return the IDs of the recently commented posts. Check this StackOverflow thread that explains how to get these IDs https://wordpress.stackexchange.com/questions/105534/how-to-get-most-recent-commented-post-above-new-submitted-post-in-wordpress
Then pass the results of this shortcode to a view using an argument:

[wpv-view name="my-view" ids="[my-last-commented-posts-ids]"]

The view needs to be filtered by the IDs passed in a shortcode argument.

Or you may use a 3rd party plugin such as https://wordpress.org/plugins/basic-recent-commented-posts-widget/

Relevant Documentation:
https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/

This support ticket is created 4 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 3 replies, has 2 voices.

Last updated by Paul Bowman 4 years, 3 months ago.

Assisted by: Jamal.

Author
Posts
#1781875

I refer here to an April 2020 support topic dealing with an issue similar to mine. I found it helpful and was able to implement the solution proposed there with partial success.
https://toolset.com/forums/topic/building-a-view-that-lists-most-recently-commented-posts/

The part of that support topic’s proposed solution that I haven’t been able to get working has to do with making use of the comment_post hook in a way that only does the action when a comment is approved. As I’ve studied the problem, I’ve begun to think that comment_post might not be the appropriate hook for this purpose. Another, edit_comment, seemed at first like a good candidate, but the fact that, as its Codex documentation says, it “fires immediately before comment status transition hooks are fired” seems to be a problem. Whether I’m understanding everything correctly there isn’t clear to me, though. Digging further leads to a complicated set of relationships around wp_get_comment_status(), the transition_comment_status hook, and so on, that find me out of my depth.

My ideal solution, of course, would be a Views shortcode that simply let me get comment data including status when a query grabs the post it’s connected to. It doesn’t look that shortcode or set of shortcodes exists yet. Am I missing something in existing Views options that could help me here, though?

What I’ve got working right now is two functions paired with custom fields, following Jamal’s code directly except for the comment_approved condition, which I tried a variety of things with but always found prevented the function from running.

function update_last_comment_author( $comment_ID ) {
    $comment = get_comment( $comment_ID );
    $post_id = $comment->comment_post_ID ;
    $post = get_post( $post_id );
    if ( $post->post_type == "blog-personal" ) {
        update_post_meta( $post_id, 'wpcf-last-comment-author', $comment->comment_author );
    }
}
add_action( 'comment_post', 'update_last_comment_author', 10, 2 );

function update_last_comment_date( $comment_ID ) {
    $comment = get_comment( $comment_ID );
    $post_id = $comment->comment_post_ID ;
    $post = get_post( $post_id );
    if ( $post->post_type == "blog-personal" ) {
        update_post_meta( $post_id, 'wpcf-last-comment-date', time() );
    }
}
add_action( 'comment_post', 'update_last_comment_date', 10, 2 );

I’d be grateful for suggestions. I can live with it as is for now, but restricting the list by comment status would be more useful.

#1782297

Hello and thank you for contacting the Toolset support.

As you can see in the WordPress database structure, comments are stored in another table than the one for posts. Currently, Toolset does not query/create/update/delete comments. It only acts on the posts and taxonomies tables. https://codex.wordpress.org/Database_Description
There are no shortcodes that will allow you to pull the comments data, either directly, or from posts on a view's query.

I actually did not test the solution, I have suggested on that ticket. You may be right, and "comment_post" is probably not the best hook to use. "transition_comment_status" seems to be a good candidate. This pseudo-code may help:

// update post last comment field when a comment is approved
function update_last_comment_field( $new_status, $old_status, $comment ) {
    // Only when the status is approved.
    if( 1 === $new_status ){
        // get the post ID
        $post_id = $comment->comment_post_ID ;
        
		// get the post object
        $post = get_post( $post_id );
        
		// only for post type "post"
        if ( $post->post_ype == "post" ) {
            // update custom field. Toolset field slug is prefixed with "wpcf-"
            update_post_meta( $post_id, 'wpcf-last-comment-time', time() );
        }
    }
}
 
// Execute this action when a comment is saved
add_action( 'transition_comment_status', 'update_last_comment_field', 10, 3 );

I might also suggest creating a custom shortcode that will return the IDs of the recently commented posts. Check this StackOverflow thread that explains how to get these IDs https://wordpress.stackexchange.com/questions/105534/how-to-get-most-recent-commented-post-above-new-submitted-post-in-wordpress
Then pass the results of this shortcode to a view using an argument:

[wpv-view name="my-view" ids="[my-last-commented-posts-ids]"]

The view needs to be filtered by the IDs passed in a shortcode argument. https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/

Or you may use a 3rd party plugin such as https://wordpress.org/plugins/basic-recent-commented-posts-widget/

#1782829

Thanks so much, Jamal. Your guidance on converting this to the transition_comment_status hook did the trick for me — made it clear what’s going on with these status hooks. (The codex basically explains it here, I see now, although statuses are different for posts and comments: https://codex.wordpress.org/Post_Status_Transitions.) I did make the minor adjustment from “if ( 1 === $new_status )” to “if ( 'approved' === $new_status )” just so it’s a little easier to see at a glance what the function’s doing if I have to come back to it later.

Your second idea here, filtering by post IDs, is more than I need at the moment, but it offers a path to pursue to do something more thoroughgoing in this vein in future. I don’t think I was aware that the value supplied to filter a view by shortcode attribute could itself be a shortcode. That opens up a lot of possibilities, it seems like. Very valuable info.

#1782831

Much appreciated!