0

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;

}
3
  • You can use in_array() - in_array( $post->post_type, array('contacts', 'post_type', 'post_type') ).
    – Sally CJ
    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
    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
    Jan 28, 2019 at 5:50

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.