[wp-hackers] Disable comments on pages post type

Andrew Nacin wp at andrewnacin.com
Thu May 26 21:17:43 UTC 2011


On Thu, May 26, 2011 at 12:57 PM, Gavin Pearce <Gavin.Pearce at 3seven9.com>wrote:

> If you have any pages setup already, it's worth running:
>
> UPDATE `wp_posts`
> SET comment_status='closed'
> WHERE post_type='page'
>
> else you'll find some pages will still display the comments RSS feed in
> the header etc ...
>

You can also filter this the way WordPress closes comments after 14 days, on
runtime:

add_filter( 'the_posts', 'nacin_close_comments_for_pages' );
add_filter( 'comments_open', 'nacin_close_comments_for_page', 10, 2 );
add_filter( 'pings_open', 'nacin_close_comments_for_page', 10, 2 );

function nacin_close_comments_for_pages( $posts ) {
 if ( ! empty( $posts ) && is_page() ) {
 $posts[0]->comment_status = 'closed';
 $posts[0]->post_status = 'closed';
}
 return $posts;
}

function nacin_close_comments_for_page( $open, $post_id ) {
if ( ! $open )
 return $open;
$post = get_post( $post_id );
 if ( 'page' == $post->post_type )
 return false;
 return $open;
}

(Disclaimer: Untested, written on the fly.)

Nacin


More information about the wp-hackers mailing list