WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 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:

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 4 years ago.
nacin's patch, plus a workaround for get_post_type() bug
remove_default_for_id.diff (614 bytes) - added by blepoxp 4 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 4 years ago.
Default 1st param to all post types
is_post_type.diff (891 bytes) - added by scribu 4 years ago.
is_post_of_type.diff (1.2 KB) - added by scribu 4 years ago.
introduce is_post_of_type()
is_singular.diff (3.0 KB) - added by scribu 4 years ago.
add $post_type parameter to is_singular()
is_single.diff (3.0 KB) - added by scribu 4 years ago.
add $post_type parameter to is_singule()
is_singular.2.diff (3.0 KB) - added by scribu 4 years ago.
preserve old behaviour of is_singular() when not passing a post type
12588.simple.diff (1.8 KB) - added by nacin 4 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 4 years ago.
check $wp_query->is_single too
12588.2.diff (1.8 KB) - added by ryan 4 years ago.

Download all attachments as: .zip

Change History (40)

rmccue4 years ago

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

comment:1 rmccue4 years ago

  • Keywords has-patch added; needs-patch removed

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

blepoxp4 years ago

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

comment:2 blepoxp4 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.

comment:3 nacin4 years ago

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

comment:4 sirzooro4 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.

sirzooro4 years ago

Default 1st param to all post types

comment:5 nacin4 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

comment:6 scribu4 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).

scribu4 years ago

comment:7 scribu4 years ago

  • Keywords has-patch added; needs-patch removed

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

comment:8 nacin4 years ago

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

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

comment:9 chrisscott4 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.

comment:10 scribu4 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.

comment:11 follow-up: scribu4 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

comment:12 in reply to: ↑ 11 nacin4 years ago

  • Keywords needs-patch added

Replying to scribu:

Maybe have two functions:

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

scribu4 years ago

introduce is_post_of_type()

comment:13 scribu4 years ago

  • Keywords has-patch added; needs-patch removed

comment:14 follow-up: sirzooro4 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.

comment:15 sirzooro4 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.

comment:16 in reply to: ↑ 14 ; follow-up: scribu4 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...

comment:17 in reply to: ↑ 16 ; follow-ups: sirzooro4 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.

comment:18 in reply to: ↑ 17 scribu4 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.

scribu4 years ago

add $post_type parameter to is_singular()

comment:19 in reply to: ↑ 17 scribu4 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.

scribu4 years ago

add $post_type parameter to is_singule()

scribu4 years ago

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

nacin4 years ago

Aligns is_post_type and is_post_type_hierarchical with the taxonomy counterparts.

comment:20 nacin4 years ago

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

scribu4 years ago

check $wp_query->is_single too

comment:21 scribu4 years ago

  • Keywords needs-testing added

Refreshed patch: is_singular.3.diff

comment:22 nacin4 years ago

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

comment:23 scribu4 years ago

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

comment:24 scribu4 years ago

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

comment:25 anointed4 years ago

  • Cc anointed added

comment:26 scribu4 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()

comment:27 ryan4 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().

ryan4 years ago

comment:28 ryan4 years ago

Something like that (not completely tested).

comment:29 nacin4 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.