php - Allow Comments by Default for Multiple Post Types

admin2025-06-04  2

I have this working for one post type, but need to do it for three. I tried the "else" statements but I realized I suck at PHP.

function my_comments_open( $open, $post_id ) {

$post = get_post( $post_id );

if ( 'contacts' == $post->post_type )
  $open = true;

return $open;

}

I have this working for one post type, but need to do it for three. I tried the "else" statements but I realized I suck at PHP.

function my_comments_open( $open, $post_id ) {

$post = get_post( $post_id );

if ( 'contacts' == $post->post_type )
  $open = true;

return $open;

}
Share Improve this question asked Jan 28, 2019 at 4:11 AndrewAndrew 235 bronze badges 3
  • You can use in_array() - in_array( $post->post_type, array('contacts', 'post_type', 'post_type') ). – Sally CJ Commented Jan 28, 2019 at 4:52
  • Thank you for the response! So I'm taking a shot at where to put this: function my_comments_open( $open, $post_id ) { $post = get_post( $post_id ); if ( in_array( $post->post_type, array('contacts', 'companies', 'properties') ) $open = true; return $open; } Does that look correct? – Andrew Commented Jan 28, 2019 at 5:18
  • Yes, it does, except you're missing the closing ) for the if block. Anyway, check my answer. – Sally CJ Commented Jan 28, 2019 at 5:50
Add a comment  | 

1 Answer 1

Reset to default 0
function default_comments_on( $data ) 
{
    $myCustomPostType = array("my_custom_post_type_1", "my_custom_post_type_2", "contacts"); /* Custom Post Type Array */

    if (in_array($data['post_type'], $myCustomPostType))
    {
        $data['comment_status'] = 'open';
    } 

    return $data;
}
add_filter( 'wp_insert_post_data', 'default_comments_on' );

Add this code in functions.php

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748969352a315251.html

最新回复(0)