WordPress.org

Make WordPress Core

Opened 10 years ago

Closed 9 years ago

Last modified 9 years ago

#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:
PR Number:

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)

12588.diff (699 bytes) - added by rmccue 10 years ago.
nacin's patch, plus a workaround for get_post_type() bug
remove_default_for_id.diff (614 bytes) - added by blepoxp 10 years ago.
Removed lines for id and change default id from null to false so it would work with get_post_type
post.php.diff (666 bytes) - added by sirzooro 10 years ago.
Default 1st param to all post types
is_post_type.diff (891 bytes) - added by scribu 10 years ago.
is_post_of_type.diff (1.2 KB) - added by scribu 10 years ago.
introduce is_post_of_type()
is_singular.diff (3.0 KB) - added by scribu 10 years ago.
add $post_type parameter to is_singular()
is_single.diff (3.0 KB) - added by scribu 10 years ago.
add $post_type parameter to is_singule()
is_singular.2.diff (3.0 KB) - added by scribu 10 years ago.
preserve old behaviour of is_singular() when not passing a post type
12588.simple.diff (1.8 KB) - added by nacin 10 years ago.
Aligns is_post_type and is_post_type_hierarchical with the taxonomy counterparts.
is_singular.3.diff (1.8 KB) - added by scribu 10 years ago.
check $wp_query->is_single too
12588.2.diff (1.8 KB) - added by ryan 9 years ago.

Download all attachments as: .zip

Change History (40)

@rmccue
10 years ago

nacin's patch, plus a workaround for get_post_type() bug

#1 @rmccue
10 years ago

  • Keywords has-patch added; needs-patch removed

Once #12827 is fixed, we can remove the default for $id lines.

@blepoxp
10 years ago

Removed lines for id and change default id from null to false so it would work with get_post_type

#2 @blepoxp
10 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 @nacin
10 years ago

  • Keywords dev-feedback removed
  • Owner set to nacin
  • Status changed from new to accepted

#4 @sirzooro
10 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.

@sirzooro
10 years ago

Default 1st param to all post types

#5 @nacin
10 years ago

  • Resolution set to fixed
  • Status changed from accepted to closed

(In [14158]) Introduce is_post_type(). Can check if a post type is registered, or also if a post (current or specified) is of a certain post type. fixes #12588, props sirzooro, blepoxp, rmccue

#6 @scribu
10 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).

@scribu
10 years ago

#7 @scribu
10 years ago

  • Keywords has-patch added; needs-patch removed

is_post_type.diff first checks if the $id parameter is set.

#8 @nacin
10 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [14498]) Fix to is_post_type. props scribu, fixes #12588.

#9 @chrisscott
10 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

As originally committed and documented in [14158] this function defaulted to the current post ID if it wasn't passed. That's now changed in [14498]. This seems like a bug since the original commit and subsequent patches retained/fixed that functionality.

#10 @scribu
10 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: @scribu
10 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 @nacin
10 years ago

  • Keywords needs-patch added

Replying to scribu:

Maybe have two functions:

Sounds good. I've apparently been grasping at straws here.

@scribu
10 years ago

introduce is_post_of_type()

#13 @scribu
10 years ago

  • Keywords has-patch added; needs-patch removed

#14 follow-up: @sirzooro
10 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 @sirzooro
10 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: @scribu
10 years ago

Replying to sirzooro:

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.

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: @sirzooro
10 years ago

Replying to scribu:

Replying to sirzooro:

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.

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 @scribu
10 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.

@scribu
10 years ago

add $post_type parameter to is_singular()

#19 in reply to: ↑ 17 @scribu
10 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 return true. But this is not a problem - I can use is_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.

@scribu
10 years ago

add $post_type parameter to is_singule()

@scribu
10 years ago

preserve old behaviour of is_singular() when not passing a post type

@nacin
10 years ago

Aligns is_post_type and is_post_type_hierarchical with the taxonomy counterparts.

#20 @nacin
10 years ago

(In [14521]) Simplify is_post_type and is_post_type_hierarchical. Make them work like their taxonomy counterparts. see #12588.

@scribu
10 years ago

check $wp_query->is_single too

#21 @scribu
10 years ago

  • Keywords needs-testing added

Refreshed patch: is_singular.3.diff

#22 @nacin
10 years ago

I'm game for is_singular, though I'm confused -- why are we now also checking is_single?

#23 @scribu
10 years ago

Because we want is_singular('book') to return true only when there's a single book being displayed.

#24 @scribu
10 years ago

And, as somebody in IRC mentioned, we should also have is_archive('book'), to complement is_singular('book').

#25 @anointed
9 years ago

  • Cc anointed added

#26 @scribu
9 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()

#27 @ryan
9 years ago

If page is passed, then the is_single check will cause false to always be returned, it seems. I think it should return immediately if ->is_singular is false or if post_types is empty. Otherwise return the result of in_array().

@ryan
9 years ago

#28 @ryan
9 years ago

Something like that (not completely tested).

#29 @nacin
9 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [14887]) Give is_singular the ability to provide awareness/context for custom post types. props ryan, scribu. Usage: is_singular('book') or is_singular( array( 'newspapers', 'books' ) ). fixes #12588.

Note: See TracTickets for help on using tickets.