[wp-hackers] $postnow?

Andrew Nacin wp at andrewnacin.com
Wed Sep 22 06:46:22 UTC 2010

On Wed, Sep 22, 2010 at 2:32 AM, Mike Schinkel
<mikeschinkel at newclarity.net>wrote:
> My use-case?  My client wants me to disable most of the standard metaboxes
> for post_type='page' except in some cases we are going to add metaboxes, and
> we've decided to key the metabox selection logic to the page template.  But
> in order to determine the current page template I need to be able to first
> determine what the current post ID is.

I was offering advice on something similar earlier and also suggested using
the current page template.

> FYI, here's the function I wrote for this (which I actually need in
> numerous other contexts) but it seems like being able to determine the
> current post ID in a robust and reliable fashion should be someone managed
> by core?

The $id global or $post->ID or $post_id will all get you what you need.
Instead of a custom function (with no guarantee we won't break it in the
future), I think the problem is that you're relying on a hook that is too
early for what you want. admin_init runs really early on a typical admin
page. Even load-$pagenow runs too early for a lot of stuff. And even if they
weren't too early, they're too generalized and they would require you to
make all sorts of duplicative checks that the admin later makes for you.
(There's a lot of branching in post.php for example.)

There are metabox-specific hooks after the add_meta_box() calls and before
they are generated, which would be the perfect time and context to remove,
add, or adjust meta boxes.

More information about the wp-hackers mailing list