#12588 closed enhancement (fixed)
Add function to check if current page is a custom post type
Reported by: | sirzooro | Owned by: | nacin |
---|---|---|---|
Milestone: | 3.0 | Priority: | normal |
Severity: | normal | Version: | 3.0 |
Component: | Administration | Keywords: | has-patch needs-testing |
Focuses: | Cc: |
Description
From #9674:
There is one important piece missing, as Denis-de-Bernardy pointed earlier:
Following up on the previous remark... some kind of is_custom() function would be useful, and a means to bypass the WP query entirely in the workflow immediately after the query is parsed. else WP_Query may get loaded with junk. init_query_flags() for instance, probably also needs some reference to is_custom = false. after parse_query is done, $wp_query filters itself through do_action_ref_array(), and if it ends up with is_custom set to true at that stage, the actual WP query gets skipped entirely -- WP should then make the assumption that it has been set up already. I don't think we actually need anything in the template loader, since there is a hook in there already. But there definitely needs something over in the wp_query query class to go something like: please bail the WP query.
I would like to see function with following declaration, in order to be able to check if current page belongs to given post type and/or give list of custom posts to test against:
function is_custom( $post_type = '', $post_id = '' )
Attachments (11)
Change History (40)
#1
@
15 years ago
- Keywords has-patch added; needs-patch removed
Once #12827 is fixed, we can remove the default for $id
lines.
@
15 years ago
Removed lines for id and change default id from null to false so it would work with get_post_type
#2
@
15 years ago
- Cc glenn@… added
- Keywords dev-feedback added
Was looking through tickets, saw that other ticket was closed.
Removed lines per ryan's note, tested, changed $id=null to $id=false so it is compatible with get_post_type($the_post=false)
Let me know if anything else needs changed.
#3
@
15 years ago
- Keywords dev-feedback removed
- Owner set to nacin
- Status changed from new to accepted
#4
@
15 years ago
I would like to be able to check if current page is of any registered post type. I have modified last patch a bit - with my change 1st function parameter is optional too and defaults to all registered post types.
#6
@
14 years ago
- Keywords needs-patch added; has-patch removed
- Resolution fixed deleted
- Status changed from closed to reopened
is_post_type() doesn't work when trying to "Checks if a post type is registered":
function _debug() { var_dump(is_post_type('post'), (bool) get_post_type_object('post')); } add_action('init', '_debug', 100);
You get bool(false) bool(true).
#7
@
14 years ago
- Keywords has-patch added; needs-patch removed
is_post_type.diff first checks if the $id parameter is set.
#10
@
14 years ago
- Keywords has-patch removed
Then the documentation should be updated to reflect that is_post_type() only checks if a post if of a certain type.
#11
follow-up:
↓ 12
@
14 years ago
Maybe have two functions:
is_post_type() - check if a post type is registered
is_post_of_type() - check if a certain post is of a certain type
#12
in reply to:
↑ 11
@
14 years ago
- Keywords needs-patch added
Replying to scribu:
Maybe have two functions:
Sounds good. I've apparently been grasping at straws here.
#14
follow-up:
↓ 16
@
14 years ago
I agree, new approach is much better.
BTW, please make sure is_post_of_type()
will return false
when called from non-post pages (e.g. home page, category or date archive). I am going to test this within few next days and will let you know if there are any issues.
#15
@
14 years ago
One more thing: previous function version allowed to skip $types
param, and uses all post types (returned by get_post_types()
) as default. Please change is_post_of_type()
to follow this.
#16
in reply to:
↑ 14
;
follow-up:
↓ 17
@
14 years ago
Replying to sirzooro:
BTW, please make sure
is_post_of_type()
will returnfalse
when called from non-post pages (e.g. home page, category or date archive). I am going to test this within few next days and will let you know if there are any issues.
Why?
One more thing: previous function version allowed to skip $types param, and uses all post types (returned by get_post_types()) as default. Please change is_post_of_type() to follow this.
I disagree. It's meant to check agains specific post types; that's why it's called is_post_of_type() you know...
#17
in reply to:
↑ 16
;
follow-ups:
↓ 18
↓ 19
@
14 years ago
Replying to scribu:
Replying to sirzooro:
BTW, please make sure
is_post_of_type()
will returnfalse
when called from non-post pages (e.g. home page, category or date archive). I am going to test this within few next days and will let you know if there are any issues.
Why?
I would like to use following code in plugin:
if ( is_post_of_type( 'mytype' ) { do_something_special(); }
I think other people would like to do the same.
One more thing: previous function version allowed to skip $types param, and uses all post types (returned by get_post_types()) as default. Please change is_post_of_type() to follow this.
I disagree. It's meant to check agains specific post types; that's why it's called is_post_of_type() you know...
I need this to do some things for all post types, e.g. print meta headers in <head>, which are stored as post meta data. The easiest way would be to just check if is_post_of_type()
will return true
. But this is not a problem - I can use is_post_of_type( get_post_types() )
instead.
#18
in reply to:
↑ 17
@
14 years ago
- Type changed from defect (bug) to enhancement
I think I understand what you're getting at:
Rather than checking if a post type is registered, you want is_post_type() to act like a conditional tag.
IMO, we should add a $post_type parameter to is_singular() instead.
#19
in reply to:
↑ 17
@
14 years ago
Replying to sirzooro:
One more thing: previous function version allowed to skip $types param, and uses all post types (returned by get_post_types()) as default. Please change is_post_of_type() to follow this.
I disagree. It's meant to check agains specific post types; that's why it's called is_post_of_type() you know...
I need this to do some things for all post types, e.g. print meta headers in <head>, which are stored as post meta data. The easiest way would be to just check if
is_post_of_type()
will returntrue
. But this is not a problem - I can useis_post_of_type( get_post_types() )
instead.
You should be able to use is_single() instead. From the doc:
* This applies to other post types, attachments, pages, posts. Just means that * the current query has only a single object.
#22
@
14 years ago
I'm game for is_singular, though I'm confused -- why are we now also checking is_single?
#23
@
14 years ago
Because we want is_singular('book') to return true only when there's a single book being displayed.
#24
@
14 years ago
And, as somebody in IRC mentioned, we should also have is_archive('book'), to complement is_singular('book').
#26
@
14 years ago
Having is_post_type() and is_taxonomy() is confusing, since the is_*() functions are expected to be conditional tags. I think we should rename (deprecate) them:
is_taxonomy() -> taxonomy_exists()
is_taxonomy_hierarchical() -> hierarchical_taxonomy_exists()
is_post_type() -> post_type_exists()
is_hierarchical_post_type() -> hierarchical_post_type_exists()
nacin's patch, plus a workaround for get_post_type() bug