Support » Developing with WordPress » Limiting comments to a certain amounts of days

  • I want to limit the visibility of comments under a page (or even the entire website) to a fixed number of days. Say 7 days. If the comment is older it should not be visible.
    I first tried searching for an existing plugin to do the job but no success.

    So I tried to come up with some solution myself.
    I managed to get my own query in a plugin to work with a shortcode and list the comments that were within the clause. But I also needed a form to add new comments. Therefor I need the comments to be activated on the page, But this will also show the WP native comments and form. If I disable the comments on the page my own form is also gone.

    I don’t dislike the native comments, they are fine by me, so I tried to add filters to the plugin instead of my own list but it had no effect on the native comments. Older comments are still being shown.

    Has one of you an idea on how to approach this problem, a plugin or a snippet of code that can help me further?

    If I had to program it myself from scratch it would have been such a problem but WordPress is just a bit too complicated for me at times. 🙁
    The code I tried is still on my work-computer. This code didn’t work but could be handy as a reference for what I did try. I could post it tomorrow.

    Thank you in advance for taking the time to hear me out.


    • This topic was modified 1 week, 6 days ago by janneman63.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter janneman63

    (@janneman63)

    function wpb_filter_comments_template( $template ) {
        global $wp_query;
        $wp_query->query_vars['date_query'] = array(
            array(
                'after' => '7 days ago',
            ),
        );
        return $template;
    }
    add_filter( 'comments_template', 'wpb_filter_comments_template' );

    This is what I came up with. comments_template was a second try I first tried something with pre_get_comments but nothing seems to have any effect on the native WP comments.

    Moderator bcworkz

    (@bcworkz)

    TBH I don’t see how that would accomplish what you want. If it works for you, then great. To me it looks like it’d restrict posts found, not comments.

    You have sort of hit on why pre_get_comments wasn’t working for you. If your theme template is using wp_list_comments(), it doesn’t necessarily make a new comment query where pre_get_comments would be effective. It will often instead get the comments from the $wp_query object.

    If the comments are coming from $wp_query->comments, you could use “the_posts” filter to alter the associated comments. Much like your use of “comments_template”, we’re not using the filter for its intended purpose, but it serves our need all the same. The current $wp_query object is passed by reference, so we can alter what’s in $wp_query->comments as we wish. The comments have already been found, you would loop through them and unset any that do not fit your criteria.

    To be thorough, you may want to also utilize “pre_get_comments” as well. Even though it isn’t effective in this context, it may be effective in another context where you want the same restriction.

    Thread Starter janneman63

    (@janneman63)

    @bcworkz Thank you for your reply.
    I managed to request the comments and display only the ones that fulfill my criteria but it did’t stop WP from displaying the normal comments also.
    disabling the comments for the page did list my own list alone but also removed the form for new comments.
    I think I now should look at the theme files, to see if I can change the behavior there. If I can prevent the listing in the Theme but allow the form then a plug-in can list the comments I want to display.
    I have no need for nesting or comments on comments so that should be quite straightforward.
    Thanks again for your thoughts.

    • This reply was modified 1 week, 2 days ago by janneman63.
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.