$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Allow Comments by Default for Multiple Post Types|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Allow Comments by Default for Multiple Post Types

matteradmin8PV0评论

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

Post a comment

comment list (0)

  1. No comments so far