WordPress.org

Support

Support » How-To and Troubleshooting » Automatically disabling comments on custom post types

Automatically disabling comments on custom post types

  • Is there a way to automatically disable comments on custom post types? Specifically, I’d like it to adhere to the Settings -> Discussion that reads “Automatically close comments on articles older than XX days”.

    Or is this something I have to add code for manually?

Viewing 5 replies - 1 through 5 (of 5 total)
  • There’s no built in functionality for this, but Google revealed this tutorial on WPRecipes which will probably fit your needs nicely.

    Apologies, I forgot to add that you could easily modify this to query for post type (or whatever other metric you’d like) instead of date.

    Alternatively, I supposed you could edit the post type to not support comments, but I’m guessing in your case this may be more of a hassle.

    Thank you Rebecca. Are you aware of any way to access the variable for what it says in the admin panel and use that instead of manually entering the number of days?

    Answered my own question.

    <?php echo get_option( ‘close_comments_days_old’ ); ?>

    Here's the complete snippet I used.
    
    // Automatically close comments older than a certain number of days based
    // on setting in admin panel for discussion
    function close_comments( $posts ) {
    	if ( !is_single() ) { return $posts; }
    
    	if ( 'enter_my_post_type' == get_post_type($posts[0]->ID) ) {
    		if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( get_option( 'close_comments_days_old' ) * 24 * 60 * 60 ) ) {
    			$posts[0]->comment_status = 'closed';
    			$posts[0]->ping_status    = 'closed';
    			wp_update_post( $posts[0] );
    		}
    	}
    	return $posts;
    }
    add_filter( 'the_posts', 'close_comments' );
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Automatically disabling comments on custom post types’ is closed to new replies.
Skip to toolbar