WordPress.org

Ready to get started?Download WordPress

Forums

Automatically disabling comments on custom post types (6 posts)

  1. dmorda
    Member
    Posted 1 year ago #

    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?

  2. Rebecca Schaefer
    Member
    Posted 1 year ago #

    There's no built in functionality for this, but Google revealed this tutorial on WPRecipes which will probably fit your needs nicely.

  3. Rebecca Schaefer
    Member
    Posted 1 year ago #

    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.

  4. dmorda
    Member
    Posted 1 year ago #

    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?

  5. dmorda
    Member
    Posted 1 year ago #

    Answered my own question.

    <?php echo get_option( 'close_comments_days_old' ); ?>

  6. dmorda
    Member
    Posted 1 year ago #

    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' );

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.