views:

69

answers:

1
$this->add_meta_box( 'select_post_template', __( 'Post Template', 'custom-post-templates' ), 'select_post_template', 'post', 'side', 'default' );

To make a plugin work with custom post types, I've been told to change "post" to the name of the custom post type. Does anyone know if I can make it work with all custom post types (including regular posts) by changing this line somehow?

FYI, I found this at: http://wordpress.org/support/topic/custom-post-templates-with-custom-post-types-in-wp-30?replies=5#post-1679398

And it's in reference to the Custom Post Template plugin: http://wordpress.org/extend/plugins/custom-post-template/

Thanks in advance!

EDIT:

I've tried:

$post_types = get_post_types(array("public" => true));
foreach ($post_types as $post_type) {
  $this->add_meta_box("select_post_template", __("Post Template", "custom-post-templates"), "select_post_template", $post_type, "side", "default");
}

But the custom post types still don't get the template selection menu. The posts do, just as they did with the original code. Thanks for suggestion... does anyone have another?

Note: Conceptually, the approach is solid. If I create my own array with a list of my custom post types, this code does add the templating to them.

+1  A: 

You can loop through all the registered post types and add the meta box for each, although you may need to filter out some types as attachments are also posts.

$post_types = get_post_types(array("public" => true));
foreach ($post_types as $post_type) {
  add_meta_box("select_post_template", __("Post Template", "custom-post-templates"), "select_post_template", $post_type, "side", "default");
}

With regards specifically to the Custom Post Template plugin, I think the problem is that your custom post types are registered after it's initialised (as it doesn't use a hook). So, $post_types (above) doesn't contain your types and meta boxes cannot be added for them. You could try adding this hack (at the end of custom-post-templates.php):

add_action('init', 'hack_add_meta_boxes');
function hack_add_meta_boxes() {
  global $CustomPostTemplates;
  $post_types = get_post_types(array('public' => true));
  foreach ($post_types as $post_type) {
    $CustomPostTemplates->add_meta_box( 'select_post_template', __( 'Post Template', 'custom-post-templates' ), 'select_post_template', $post_type, 'side', 'default' );
  }
}
Richard M
Thanks for trying, but it didn't expand to the custom post types :(
Matrym
I think your problem is probably that your custom post types are being registered after the Custom Post Template plugin is initialised (as it doesn't use a hook). So,`$post_types` doesn't contain your post types and meta boxes are not added for them.
Richard M