Custom Post Types

Hi @ned–quick question about the custom post types filter added to 4.0. If I wanted to add a custom post type to the array, what is is that I need to do now. I was planning to create a functionality plugin that looks like this:

function add_post_types ( $existing_post_types = array()) {
$add_custom_types = array (
‘glossary’ // or other post types we want to add to the permitted list
);
return array_merge( $add_custom_types, $existing_post_types );
}

add_filter( ‘pb_supported_post_types’, ‘add_post_types’ );

?>

Does that look about right?

Updated: the code above appears to be working for us on our dev server …

1 Like

My only suggestion would be to namespace your function as add_post_types() is pretty generic and could conflict with another plugin. Also you can make it a little more concise, like so:

function steel_add_post_types ( $post_types ) {
  return array_merge( $post_types, [ 'glossary' ] );
}

add_filter( 'pb_supported_post_types', 'steel_add_post_types' );
1 Like

Thanks Ned–I’ll rename the function. I built the array because we were planning to add more than one custom post type to our actual plugin and I don’t think array_merge can take more than two arguments, but the concision is appreciated.

array_merge() only takes two arguments but the second is an array. So you could change it like so to add another post type:

function steel_add_post_types ( $post_types ) {
  return array_merge( $post_types, [ 'glossary', 'other-post-type' ] );
}

add_filter( 'pb_supported_post_types', 'steel_add_post_types' );
1 Like

Nice–that makes good sense. Thanks again, Ned!