get_post_class( string|string[] $css_class = '', int|WP_Post|null $post = null ): string[]
- Since
- 2.7.0, 4.2.0
- Source
wp-includes/post-template.php:494
Description
The class names are many:
- If the post has a post thumbnail,
has-post-thumbnailis added as a class. - If the post is sticky, then the
stickyclass name is added. - The class
hentryis always added to each post. - For each taxonomy that the post belongs to, a class will be added of the format
{$taxonomy}-{$slug}, e.g.category-fooormy_custom_taxonomy-bar.
Thepost_tagtaxonomy is a special case; the class has thetag-prefix instead ofpost_tag-.
All class names are passed through the filter, 'post_class', followed by $css_class parameter value, with the post ID as the last parameter.
Compatibility
- WordPress
- since 4.2.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$css_classstring|string[]optional- Space-separated string or array of class names to add to the class list. Default empty.Default:
'' $postint|WP_Post|nulloptional- Post ID or post object.Default:
null
Return value
string[]- Array of class names.
Performance profile
How much work a call to get_post_class() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 11–141
- Plugin surface
- 2 hooks
- Called by
- 4
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 216.
Third-party callbacks on 'post_class_taxonomies', 'post_class' run inside this call, and their cost is not bounded by anything here.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()one call below get_post_class() - cacheobject cache
wp_cache_add()one call below get_post_class()
Further down the call graph this can also reach serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 54 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_post_class() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 11 | get_post() |
!post_type_supports() && !current_theme_supports() && !is_sticky() | 93–99 | get_post(), is_admin(), post_type_supports(), post_password_required(), current_theme_supports(), is_sticky(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
!post_type_supports() && current_theme_supports() && !has_post_thumbnail() && !is_sticky() | 99–105 | get_post(), is_admin(), post_type_supports(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_sticky(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
!post_type_supports() && !current_theme_supports() && is_sticky() && !is_home() | 99–107 | get_post(), is_admin(), post_type_supports(), post_password_required(), current_theme_supports(), is_sticky(), is_home(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
is_array($css_class) && is_admin() && !post_type_supports() && empty($post) && !current_theme_supports() && !is_sticky() | 100–101 | get_post(), array_map(), is_admin(), post_type_supports(), post_password_required(), current_theme_supports(), is_sticky(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && !current_theme_supports() && !is_sticky() | 102–108 | get_post(), is_admin(), post_type_supports(), get_post_format(), post_password_required(), current_theme_supports(), is_sticky(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
!post_type_supports() && !current_theme_supports() && is_sticky() && is_home() && !is_paged() | 102–108 | get_post(), is_admin(), post_type_supports(), post_password_required(), current_theme_supports(), is_sticky(), is_home(), is_paged(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
!post_type_supports() && !current_theme_supports() && is_sticky() && is_home() && is_paged() | 102–110 | get_post(), is_admin(), post_type_supports(), post_password_required(), current_theme_supports(), is_sticky(), is_home(), is_paged(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
!post_type_supports() && current_theme_supports() && has_post_thumbnail() && !is_sticky() | 103–112 | get_post(), is_admin(), post_type_supports(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_attachment(), is_sticky(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
!post_type_supports() && current_theme_supports() && !has_post_thumbnail() && is_sticky() && !is_home() | 105–113 | get_post(), is_admin(), post_type_supports(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_sticky(), is_home(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && is_wp_error() && !current_theme_supports() && !is_sticky() | 106–112 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), post_password_required(), current_theme_supports(), is_sticky(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
is_array($css_class) && is_admin() && !post_type_supports() && empty($post) && current_theme_supports() && !has_post_thumbnail() && !is_sticky() | 106–107 | get_post(), array_map(), is_admin(), post_type_supports(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_sticky(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
42 further outcomes, up to 141 instructions
is_array($css_class) && is_admin() && !post_type_supports() && empty($post) && !current_theme_supports() && is_sticky() && !is_home() | 106–109 | get_post(), array_map(), is_admin(), post_type_supports(), post_password_required(), current_theme_supports(), is_sticky(), is_home(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && current_theme_supports() && !has_post_thumbnail() && !is_sticky() | 108–114 | get_post(), is_admin(), post_type_supports(), get_post_format(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_sticky(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
!post_type_supports() && current_theme_supports() && !has_post_thumbnail() && is_sticky() && is_home() && !is_paged() | 108–114 | get_post(), is_admin(), post_type_supports(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_sticky(), is_home(), is_paged(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && !current_theme_supports() && is_sticky() && !is_home() | 108–116 | get_post(), is_admin(), post_type_supports(), get_post_format(), post_password_required(), current_theme_supports(), is_sticky(), is_home(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
!post_type_supports() && current_theme_supports() && !has_post_thumbnail() && is_sticky() && is_home() && is_paged() | 108–116 | get_post(), is_admin(), post_type_supports(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_sticky(), is_home(), is_paged(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
is_array($css_class) && is_admin() && !post_type_supports() && empty($post) && !current_theme_supports() && is_sticky() && is_home() && !is_paged() | 109–110 | get_post(), array_map(), is_admin(), post_type_supports(), post_password_required(), current_theme_supports(), is_sticky(), is_home(), is_paged(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
!post_type_supports() && current_theme_supports() && has_post_thumbnail() && is_sticky() && !is_home() | 109–120 | get_post(), is_admin(), post_type_supports(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_attachment(), is_sticky(), is_home(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
is_array($css_class) && is_admin() && !post_type_supports() && empty($post) && !current_theme_supports() && is_sticky() && is_home() && is_paged() | 109–112 | get_post(), array_map(), is_admin(), post_type_supports(), post_password_required(), current_theme_supports(), is_sticky(), is_home(), is_paged(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && !current_theme_supports() && is_sticky() && is_home() && !is_paged() | 111–117 | get_post(), is_admin(), post_type_supports(), get_post_format(), post_password_required(), current_theme_supports(), is_sticky(), is_home(), is_paged(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && !is_wp_error() && !current_theme_supports() && !is_sticky() | 111–117 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), sanitize_html_class(), post_password_required(), current_theme_supports(), is_sticky(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && !current_theme_supports() && is_sticky() && is_home() && is_paged() | 111–119 | get_post(), is_admin(), post_type_supports(), get_post_format(), post_password_required(), current_theme_supports(), is_sticky(), is_home(), is_paged(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && current_theme_supports() && has_post_thumbnail() && !is_sticky() | 112–121 | get_post(), is_admin(), post_type_supports(), get_post_format(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_attachment(), is_sticky(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && is_wp_error() && current_theme_supports() && !has_post_thumbnail() && !is_sticky() | 112–118 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_sticky(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
!post_type_supports() && current_theme_supports() && has_post_thumbnail() && is_sticky() && is_home() && !is_paged() | 112–121 | get_post(), is_admin(), post_type_supports(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_attachment(), is_sticky(), is_home(), is_paged(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && is_wp_error() && !current_theme_supports() && is_sticky() && !is_home() | 112–120 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), post_password_required(), current_theme_supports(), is_sticky(), is_home(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
!post_type_supports() && current_theme_supports() && has_post_thumbnail() && is_sticky() && is_home() && is_paged() | 112–123 | get_post(), is_admin(), post_type_supports(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_attachment(), is_sticky(), is_home(), is_paged(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && current_theme_supports() && !has_post_thumbnail() && is_sticky() && !is_home() | 114–122 | get_post(), is_admin(), post_type_supports(), get_post_format(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_sticky(), is_home(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && is_wp_error() && !current_theme_supports() && is_sticky() && is_home() && !is_paged() | 115–121 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), post_password_required(), current_theme_supports(), is_sticky(), is_home(), is_paged(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && is_wp_error() && !current_theme_supports() && is_sticky() && is_home() && is_paged() | 115–123 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), post_password_required(), current_theme_supports(), is_sticky(), is_home(), is_paged(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && is_wp_error() && current_theme_supports() && has_post_thumbnail() && !is_sticky() | 116–125 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_attachment(), is_sticky(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && current_theme_supports() && !has_post_thumbnail() && is_sticky() && is_home() && !is_paged() | 117–123 | get_post(), is_admin(), post_type_supports(), get_post_format(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_sticky(), is_home(), is_paged(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && !is_wp_error() && current_theme_supports() && !has_post_thumbnail() && !is_sticky() | 117–123 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), sanitize_html_class(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_sticky(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && current_theme_supports() && !has_post_thumbnail() && is_sticky() && is_home() && is_paged() | 117–125 | get_post(), is_admin(), post_type_supports(), get_post_format(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_sticky(), is_home(), is_paged(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && !is_wp_error() && !current_theme_supports() && is_sticky() && !is_home() | 117–125 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), sanitize_html_class(), post_password_required(), current_theme_supports(), is_sticky(), is_home(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && current_theme_supports() && has_post_thumbnail() && is_sticky() && !is_home() | 118–129 | get_post(), is_admin(), post_type_supports(), get_post_format(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_attachment(), is_sticky(), is_home(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && is_wp_error() && current_theme_supports() && !has_post_thumbnail() && is_sticky() && !is_home() | 118–126 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_sticky(), is_home(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && !is_wp_error() && !current_theme_supports() && is_sticky() && is_home() && !is_paged() | 120–126 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), sanitize_html_class(), post_password_required(), current_theme_supports(), is_sticky(), is_home(), is_paged(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && !is_wp_error() && !current_theme_supports() && is_sticky() && is_home() && is_paged() | 120–128 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), sanitize_html_class(), post_password_required(), current_theme_supports(), is_sticky(), is_home(), is_paged(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && current_theme_supports() && has_post_thumbnail() && is_sticky() && is_home() && !is_paged() | 121–130 | get_post(), is_admin(), post_type_supports(), get_post_format(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_attachment(), is_sticky(), is_home(), is_paged(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && is_wp_error() && current_theme_supports() && !has_post_thumbnail() && is_sticky() && is_home() && !is_paged() | 121–127 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_sticky(), is_home(), is_paged(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && !is_wp_error() && current_theme_supports() && has_post_thumbnail() && !is_sticky() | 121–130 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), sanitize_html_class(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_attachment(), is_sticky(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && current_theme_supports() && has_post_thumbnail() && is_sticky() && is_home() && is_paged() | 121–132 | get_post(), is_admin(), post_type_supports(), get_post_format(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_attachment(), is_sticky(), is_home(), is_paged(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && is_wp_error() && current_theme_supports() && !has_post_thumbnail() && is_sticky() && is_home() && is_paged() | 121–129 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_sticky(), is_home(), is_paged(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && is_wp_error() && current_theme_supports() && has_post_thumbnail() && is_sticky() && !is_home() | 122–133 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_attachment(), is_sticky(), is_home(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && !is_wp_error() && current_theme_supports() && !has_post_thumbnail() && is_sticky() && !is_home() | 123–131 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), sanitize_html_class(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_sticky(), is_home(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && is_wp_error() && current_theme_supports() && has_post_thumbnail() && is_sticky() && is_home() && !is_paged() | 125–134 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_attachment(), is_sticky(), is_home(), is_paged(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && is_wp_error() && current_theme_supports() && has_post_thumbnail() && is_sticky() && is_home() && is_paged() | 125–136 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_attachment(), is_sticky(), is_home(), is_paged(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && !is_wp_error() && current_theme_supports() && !has_post_thumbnail() && is_sticky() && is_home() && !is_paged() | 126–132 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), sanitize_html_class(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_sticky(), is_home(), is_paged(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && !is_wp_error() && current_theme_supports() && !has_post_thumbnail() && is_sticky() && is_home() && is_paged() | 126–134 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), sanitize_html_class(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_sticky(), is_home(), is_paged(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && !is_wp_error() && current_theme_supports() && has_post_thumbnail() && is_sticky() && !is_home() | 127–138 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), sanitize_html_class(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_attachment(), is_sticky(), is_home(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && !is_wp_error() && current_theme_supports() && has_post_thumbnail() && is_sticky() && is_home() && !is_paged() | 130–139 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), sanitize_html_class(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_attachment(), is_sticky(), is_home(), is_paged(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
post_type_supports() && !is_wp_error() && current_theme_supports() && has_post_thumbnail() && is_sticky() && is_home() && is_paged() | 130–141 | get_post(), is_admin(), post_type_supports(), get_post_format(), is_wp_error(), sanitize_html_class(), post_password_required(), current_theme_supports(), has_post_thumbnail(), is_attachment(), is_sticky(), is_home(), is_paged(), is_admin(), get_taxonomies(), apply_filters(), array_map(), apply_filters(), array_unique(), array_values() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 216 | 11–141 | 26 | |
| 8.5 | 216 | 11–141 | 26 | |
| 8.4 | 216 | 11–141 | 26 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 221 | 93–141 | 26 | |
| 8.2 | 221 | 93–141 | 26 | |
| 8.1 | 221 | 93–141 | 26 | |
| 7.4 | 221 | 93–141 | 26 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 2
2 hooks fire while get_post_class() runs, in this order:
- apply_filters( post_class_taxonomies )filterline 572 (+78 into the body)
Filters the taxonomies to generate classes for each individual term.
- apply_filters( post_class )filterline 607 (+113 into the body)
Filters the list of CSS class names for the current post.
Uses · 17
- get_post()Retrieves post data given a post ID or post object.
- is_admin()Determines whether the current request is for an administrative interface page.
- post_type_supports()Checks a post type's support for a given feature.
- get_post_format()Retrieve the format slug for a post
- is_wp_error()Checks whether the given variable is a WordPress Error.
- sanitize_html_class()Sanitizes an HTML classname to ensure it only contains valid characters.
- post_password_required()Determines whether the post requires password and whether a correct password has been provided.
- current_theme_supports()Checks a theme's support for a given feature.
- has_post_thumbnail()Determines whether a post has an image attached.
- is_attachment()Determines whether the query is for an existing attachment page.
- is_sticky()Determines whether a post is sticky.
- is_home()Determines whether the query is for the blog homepage.
Show all 17
- is_paged()Determines whether the query is for a paged result and not for the first page.
- get_taxonomies()Retrieves a list of registered taxonomy names or objects.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- is_object_in_taxonomy()Determines if the given object type is associated with the given taxonomy.
- get_the_terms()Retrieves the terms of the taxonomy that are attached to the post.
Used by · 4
- WP_Posts_List_Table::single_row()
- WP_REST_Posts_Controller::prepare_item_for_response()Prepares a single post output for response.
- post_class()Displays the classes for the post container element.
- render_block_core_post_template()Renders the `core/post-template` block on the server.
Source code
function get_post_class( $css_class = '', $post = null ) { $post = get_post( $post ); $classes = array(); if ( $css_class ) { if ( ! is_array( $css_class ) ) { $css_class = preg_split( '#\s+#', $css_class ); } $classes = array_map( 'esc_attr', $css_class ); } else { // Ensure that we always coerce class to being an array. $css_class = array(); } if ( ! $post ) { return $classes; } $classes[] = 'post-' . $post->ID; if ( ! is_admin() ) { $classes[] = $post->post_type; } $classes[] = 'type-' . $post->post_type; $classes[] = 'status-' . $post->post_status; // Post Format. if ( post_type_supports( $post->post_type, 'post-formats' ) ) { $post_format = get_post_format( $post->ID ); if ( $post_format && ! is_wp_error( $post_format ) ) { $classes[] = 'format-' . sanitize_html_class( $post_format ); } else { $classes[] = 'format-standard'; } } $post_password_required = post_password_required( $post->ID ); // Post requires password. if ( $post_password_required ) { $classes[] = 'post-password-required'; } elseif ( ! empty( $post->post_password ) ) { $classes[] = 'post-password-protected'; } // Post thumbnails. if ( current_theme_supports( 'post-thumbnails' ) && has_post_thumbnail( $post->ID ) && ! is_attachment( $post ) && ! $post_password_required ) { $classes[] = 'has-post-thumbnail'; } // Sticky for Sticky Posts. if ( is_sticky( $post->ID ) ) { if ( is_home() && ! is_paged() ) { $classes[] = 'sticky'; } elseif ( is_admin() ) { $classes[] = 'status-sticky'; } } // hentry for hAtom compliance. $classes[] = 'hentry'; // All public taxonomies. $taxonomies = get_taxonomies( array( 'public' => true ) ); /** * Filters the taxonomies to generate classes for each individual term. * * Default is all public taxonomies registered to the post type. * * @since 6.1.0 * * @param string[] $taxonomies List of all taxonomy names to generate classes for. * @param int $post_id The post ID. * @param string[] $classes An array of post class names. * @param string[] $css_class An array of additional class names added to the post. */ $taxonomies = apply_filters( 'post_class_taxonomies', $taxonomies, $post->ID, $classes, $css_class );Changelog
Introduced in 2.7.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$post retyped from int|WP_Post to int|WP_Post|null.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/post-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.