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 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 4 years, 3 months ago.
Assisted by: Jamal.