get_post_class

The timeline below displays how wordpress function get_post_class has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.2

/**
 * Retrieves an array of the class names for the post container element.
 *
 * The class names are many:
 *
 *  - If the post has a post thumbnail, `has-post-thumbnail` is added as a class.
 *  - If the post is sticky, then the `sticky` class name is added.
 *  - The class `hentry` is 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-foo` or `my_custom_taxonomy-bar`.
 *    The `post_tag` taxonomy is a special case; the class has the `tag-` prefix
 *    instead of `post_tag-`.
 *
 * All class names are passed through the filter, {@see 'post_class'}, followed by
 * `$css_class` parameter value, with the post ID as the last parameter.
 *
 * @since 2.7.0
 * @since 4.2.0 Custom taxonomy class names were added.
 *
 * @param string|string[] $css_class Optional. Space-separated string or array of class names
 *                                   to add to the class list. Default empty.
 * @param int|WP_Post     $post      Optional. Post ID or post object.
 * @return string[] Array of class names.
 */
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);
    foreach ((array) $taxonomies as $taxonomy) {
        if (is_object_in_taxonomy($post->post_type, $taxonomy)) {
            foreach ((array) get_the_terms($post->ID, $taxonomy) as $term) {
                if (empty($term->slug)) {
                    continue;
                }
                $term_class = sanitize_html_class($term->slug, $term->term_id);
                if (is_numeric($term_class) || !trim($term_class, '-')) {
                    $term_class = $term->term_id;
                }
                // 'post_tag' uses the 'tag' prefix for backward compatibility.
                if ('post_tag' === $taxonomy) {
                    $classes[] = 'tag-' . $term_class;
                } else {
                    $classes[] = sanitize_html_class($taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id);
                }
            }
        }
    }
    $classes = array_map('esc_attr', $classes);
    /**
     * Filters the list of CSS class names for the current post.
     *
     * @since 2.7.0
     *
     * @param string[] $classes   An array of post class names.
     * @param string[] $css_class An array of additional class names added to the post.
     * @param int      $post_id   The post ID.
     */
    $classes = apply_filters('post_class', $classes, $css_class, $post->ID);
    return array_unique($classes);
}

WordPress Version: 6.1

/**
 * Retrieves an array of the class names for the post container element.
 *
 * The class names are many. If the post is a sticky, then the 'sticky'
 * class name. The class 'hentry' is always added to each post. If the post has a
 * post thumbnail, 'has-post-thumbnail' is added as a class. For each taxonomy that
 * the post belongs to, a class will be added of the format '{$taxonomy}-{$slug}' -
 * eg 'category-foo' or 'my_custom_taxonomy-bar'.
 *
 * The 'post_tag' taxonomy is a special
 * case; the class has the 'tag-' prefix instead of 'post_tag-'. All class names are
 * passed through the filter, {@see 'post_class'}, with the list of class names, followed by
 * $class parameter value, with the post ID as the last parameter.
 *
 * @since 2.7.0
 * @since 4.2.0 Custom taxonomy class names were added.
 *
 * @param string|string[] $class Space-separated string or array of class names to add to the class list.
 * @param int|WP_Post     $post  Optional. Post ID or post object.
 * @return string[] Array of class names.
 */
function get_post_class($class = '', $post = null)
{
    $post = get_post($post);
    $classes = array();
    if ($class) {
        if (!is_array($class)) {
            $class = preg_split('#\s+#', $class);
        }
        $classes = array_map('esc_attr', $class);
    } else {
        // Ensure that we always coerce class to being an array.
        $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[] $class      An array of additional class names added to the post.
     */
    $taxonomies = apply_filters('post_class_taxonomies', $taxonomies, $post->ID, $classes, $class);
    foreach ((array) $taxonomies as $taxonomy) {
        if (is_object_in_taxonomy($post->post_type, $taxonomy)) {
            foreach ((array) get_the_terms($post->ID, $taxonomy) as $term) {
                if (empty($term->slug)) {
                    continue;
                }
                $term_class = sanitize_html_class($term->slug, $term->term_id);
                if (is_numeric($term_class) || !trim($term_class, '-')) {
                    $term_class = $term->term_id;
                }
                // 'post_tag' uses the 'tag' prefix for backward compatibility.
                if ('post_tag' === $taxonomy) {
                    $classes[] = 'tag-' . $term_class;
                } else {
                    $classes[] = sanitize_html_class($taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id);
                }
            }
        }
    }
    $classes = array_map('esc_attr', $classes);
    /**
     * Filters the list of CSS class names for the current post.
     *
     * @since 2.7.0
     *
     * @param string[] $classes An array of post class names.
     * @param string[] $class   An array of additional class names added to the post.
     * @param int      $post_id The post ID.
     */
    $classes = apply_filters('post_class', $classes, $class, $post->ID);
    return array_unique($classes);
}

WordPress Version: 5.5

/**
 * Retrieves an array of the class names for the post container element.
 *
 * The class names are many. If the post is a sticky, then the 'sticky'
 * class name. The class 'hentry' is always added to each post. If the post has a
 * post thumbnail, 'has-post-thumbnail' is added as a class. For each taxonomy that
 * the post belongs to, a class will be added of the format '{$taxonomy}-{$slug}' -
 * eg 'category-foo' or 'my_custom_taxonomy-bar'.
 *
 * The 'post_tag' taxonomy is a special
 * case; the class has the 'tag-' prefix instead of 'post_tag-'. All class names are
 * passed through the filter, {@see 'post_class'}, with the list of class names, followed by
 * $class parameter value, with the post ID as the last parameter.
 *
 * @since 2.7.0
 * @since 4.2.0 Custom taxonomy class names were added.
 *
 * @param string|string[] $class   Space-separated string or array of class names to add to the class list.
 * @param int|WP_Post     $post_id Optional. Post ID or post object.
 * @return string[] Array of class names.
 */
function get_post_class($class = '', $post_id = null)
{
    $post = get_post($post_id);
    $classes = array();
    if ($class) {
        if (!is_array($class)) {
            $class = preg_split('#\s+#', $class);
        }
        $classes = array_map('esc_attr', $class);
    } else {
        // Ensure that we always coerce class to being an array.
        $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));
    foreach ((array) $taxonomies as $taxonomy) {
        if (is_object_in_taxonomy($post->post_type, $taxonomy)) {
            foreach ((array) get_the_terms($post->ID, $taxonomy) as $term) {
                if (empty($term->slug)) {
                    continue;
                }
                $term_class = sanitize_html_class($term->slug, $term->term_id);
                if (is_numeric($term_class) || !trim($term_class, '-')) {
                    $term_class = $term->term_id;
                }
                // 'post_tag' uses the 'tag' prefix for backward compatibility.
                if ('post_tag' === $taxonomy) {
                    $classes[] = 'tag-' . $term_class;
                } else {
                    $classes[] = sanitize_html_class($taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id);
                }
            }
        }
    }
    $classes = array_map('esc_attr', $classes);
    /**
     * Filters the list of CSS class names for the current post.
     *
     * @since 2.7.0
     *
     * @param string[] $classes An array of post class names.
     * @param string[] $class   An array of additional class names added to the post.
     * @param int      $post_id The post ID.
     */
    $classes = apply_filters('post_class', $classes, $class, $post->ID);
    return array_unique($classes);
}

WordPress Version: 5.4

/**
 * Retrieves an array of the class names for the post container element.
 *
 * The class names are many. If the post is a sticky, then the 'sticky'
 * class name. The class 'hentry' is always added to each post. If the post has a
 * post thumbnail, 'has-post-thumbnail' is added as a class. For each taxonomy that
 * the post belongs to, a class will be added of the format '{$taxonomy}-{$slug}' -
 * eg 'category-foo' or 'my_custom_taxonomy-bar'.
 *
 * The 'post_tag' taxonomy is a special
 * case; the class has the 'tag-' prefix instead of 'post_tag-'. All class names are
 * passed through the filter, {@see 'post_class'}, with the list of class names, followed by
 * $class parameter value, with the post ID as the last parameter.
 *
 * @since 2.7.0
 * @since 4.2.0 Custom taxonomy class names were added.
 *
 * @param string|string[] $class   Space-separated string or array of class names to add to the class list.
 * @param int|WP_Post     $post_id Optional. Post ID or post object.
 * @return string[] Array of class names.
 */
function get_post_class($class = '', $post_id = null)
{
    $post = get_post($post_id);
    $classes = array();
    if ($class) {
        if (!is_array($class)) {
            $class = preg_split('#\s+#', $class);
        }
        $classes = array_map('esc_attr', $class);
    } else {
        // Ensure that we always coerce class to being an array.
        $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));
    foreach ((array) $taxonomies as $taxonomy) {
        if (is_object_in_taxonomy($post->post_type, $taxonomy)) {
            foreach ((array) get_the_terms($post->ID, $taxonomy) as $term) {
                if (empty($term->slug)) {
                    continue;
                }
                $term_class = sanitize_html_class($term->slug, $term->term_id);
                if (is_numeric($term_class) || !trim($term_class, '-')) {
                    $term_class = $term->term_id;
                }
                // 'post_tag' uses the 'tag' prefix for backward compatibility.
                if ('post_tag' == $taxonomy) {
                    $classes[] = 'tag-' . $term_class;
                } else {
                    $classes[] = sanitize_html_class($taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id);
                }
            }
        }
    }
    $classes = array_map('esc_attr', $classes);
    /**
     * Filters the list of CSS class names for the current post.
     *
     * @since 2.7.0
     *
     * @param string[] $classes An array of post class names.
     * @param string[] $class   An array of additional class names added to the post.
     * @param int      $post_id The post ID.
     */
    $classes = apply_filters('post_class', $classes, $class, $post->ID);
    return array_unique($classes);
}

WordPress Version: 5.1

/**
 * Retrieves an array of the class names for the post container element.
 *
 * The class names are many. If the post is a sticky, then the 'sticky'
 * class name. The class 'hentry' is always added to each post. If the post has a
 * post thumbnail, 'has-post-thumbnail' is added as a class. For each taxonomy that
 * the post belongs to, a class will be added of the format '{$taxonomy}-{$slug}' -
 * eg 'category-foo' or 'my_custom_taxonomy-bar'.
 *
 * The 'post_tag' taxonomy is a special
 * case; the class has the 'tag-' prefix instead of 'post_tag-'. All class names are
 * passed through the filter, {@see 'post_class'}, with the list of class names, followed by
 * $class parameter value, with the post ID as the last parameter.
 *
 * @since 2.7.0
 * @since 4.2.0 Custom taxonomy class names were added.
 *
 * @param string|string[] $class   Space-separated string or array of class names to add to the class list.
 * @param int|WP_Post     $post_id Optional. Post ID or post object.
 * @return string[] Array of class names.
 */
function get_post_class($class = '', $post_id = null)
{
    $post = get_post($post_id);
    $classes = array();
    if ($class) {
        if (!is_array($class)) {
            $class = preg_split('#\s+#', $class);
        }
        $classes = array_map('esc_attr', $class);
    } else {
        // Ensure that we always coerce class to being an array.
        $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));
    foreach ((array) $taxonomies as $taxonomy) {
        if (is_object_in_taxonomy($post->post_type, $taxonomy)) {
            foreach ((array) get_the_terms($post->ID, $taxonomy) as $term) {
                if (empty($term->slug)) {
                    continue;
                }
                $term_class = sanitize_html_class($term->slug, $term->term_id);
                if (is_numeric($term_class) || !trim($term_class, '-')) {
                    $term_class = $term->term_id;
                }
                // 'post_tag' uses the 'tag' prefix for backward compatibility.
                if ('post_tag' == $taxonomy) {
                    $classes[] = 'tag-' . $term_class;
                } else {
                    $classes[] = sanitize_html_class($taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id);
                }
            }
        }
    }
    $classes = array_map('esc_attr', $classes);
    /**
     * Filters the list of CSS class names for the current post.
     *
     * @since 2.7.0
     *
     * @param string[] $classes An array of post class names.
     * @param string[] $class   An array of additional class names added to the post.
     * @param int      $post_id The post ID.
     */
    $classes = apply_filters('post_class', $classes, $class, $post->ID);
    return array_unique($classes);
}

WordPress Version: 4.6

/**
 * Retrieves the classes for the post div as an array.
 *
 * The class names are many. If the post is a sticky, then the 'sticky'
 * class name. The class 'hentry' is always added to each post. If the post has a
 * post thumbnail, 'has-post-thumbnail' is added as a class. For each taxonomy that
 * the post belongs to, a class will be added of the format '{$taxonomy}-{$slug}' -
 * eg 'category-foo' or 'my_custom_taxonomy-bar'.
 *
 * The 'post_tag' taxonomy is a special
 * case; the class has the 'tag-' prefix instead of 'post_tag-'. All classes are
 * passed through the filter, {@see 'post_class'}, with the list of classes, followed by
 * $class parameter value, with the post ID as the last parameter.
 *
 * @since 2.7.0
 * @since 4.2.0 Custom taxonomy classes were added.
 *
 * @param string|array $class   One or more classes to add to the class list.
 * @param int|WP_Post  $post_id Optional. Post ID or post object.
 * @return array Array of classes.
 */
function get_post_class($class = '', $post_id = null)
{
    $post = get_post($post_id);
    $classes = array();
    if ($class) {
        if (!is_array($class)) {
            $class = preg_split('#\s+#', $class);
        }
        $classes = array_map('esc_attr', $class);
    } else {
        // Ensure that we always coerce class to being an array.
        $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));
    foreach ((array) $taxonomies as $taxonomy) {
        if (is_object_in_taxonomy($post->post_type, $taxonomy)) {
            foreach ((array) get_the_terms($post->ID, $taxonomy) as $term) {
                if (empty($term->slug)) {
                    continue;
                }
                $term_class = sanitize_html_class($term->slug, $term->term_id);
                if (is_numeric($term_class) || !trim($term_class, '-')) {
                    $term_class = $term->term_id;
                }
                // 'post_tag' uses the 'tag' prefix for backward compatibility.
                if ('post_tag' == $taxonomy) {
                    $classes[] = 'tag-' . $term_class;
                } else {
                    $classes[] = sanitize_html_class($taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id);
                }
            }
        }
    }
    $classes = array_map('esc_attr', $classes);
    /**
     * Filters the list of CSS classes for the current post.
     *
     * @since 2.7.0
     *
     * @param array $classes An array of post classes.
     * @param array $class   An array of additional classes added to the post.
     * @param int   $post_id The post ID.
     */
    $classes = apply_filters('post_class', $classes, $class, $post->ID);
    return array_unique($classes);
}

WordPress Version: 4.4

/**
 * Retrieve the classes for the post div as an array.
 *
 * The class names are many. If the post is a sticky, then the 'sticky'
 * class name. The class 'hentry' is always added to each post. If the post has a
 * post thumbnail, 'has-post-thumbnail' is added as a class. For each taxonomy that
 * the post belongs to, a class will be added of the format '{$taxonomy}-{$slug}' -
 * eg 'category-foo' or 'my_custom_taxonomy-bar'. The 'post_tag' taxonomy is a special
 * case; the class has the 'tag-' prefix instead of 'post_tag-'. All classes are
 * passed through the filter, 'post_class' with the list of classes, followed by
 * $class parameter value, with the post ID as the last parameter.
 *
 * @since 2.7.0
 * @since 4.2.0 Custom taxonomy classes were added.
 *
 * @param string|array $class   One or more classes to add to the class list.
 * @param int|WP_Post  $post_id Optional. Post ID or post object.
 * @return array Array of classes.
 */
function get_post_class($class = '', $post_id = null)
{
    $post = get_post($post_id);
    $classes = array();
    if ($class) {
        if (!is_array($class)) {
            $class = preg_split('#\s+#', $class);
        }
        $classes = array_map('esc_attr', $class);
    } else {
        // Ensure that we always coerce class to being an array.
        $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));
    foreach ((array) $taxonomies as $taxonomy) {
        if (is_object_in_taxonomy($post->post_type, $taxonomy)) {
            foreach ((array) get_the_terms($post->ID, $taxonomy) as $term) {
                if (empty($term->slug)) {
                    continue;
                }
                $term_class = sanitize_html_class($term->slug, $term->term_id);
                if (is_numeric($term_class) || !trim($term_class, '-')) {
                    $term_class = $term->term_id;
                }
                // 'post_tag' uses the 'tag' prefix for backward compatibility.
                if ('post_tag' == $taxonomy) {
                    $classes[] = 'tag-' . $term_class;
                } else {
                    $classes[] = sanitize_html_class($taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id);
                }
            }
        }
    }
    $classes = array_map('esc_attr', $classes);
    /**
     * Filter the list of CSS classes for the current post.
     *
     * @since 2.7.0
     *
     * @param array $classes An array of post classes.
     * @param array $class   An array of additional classes added to the post.
     * @param int   $post_id The post ID.
     */
    $classes = apply_filters('post_class', $classes, $class, $post->ID);
    return array_unique($classes);
}

WordPress Version: 4.2

/**
 * Retrieve the classes for the post div as an array.
 *
 * The class names are many. If the post is a sticky, then the 'sticky'
 * class name. The class 'hentry' is always added to each post. If the post has a
 * post thumbnail, 'has-post-thumbnail' is added as a class. For each taxonomy that
 * the post belongs to, a class will be added of the format '{$taxonomy}-{$slug}' -
 * eg 'category-foo' or 'my_custom_taxonomy-bar'. The 'post_tag' taxonomy is a special
 * case; the class has the 'tag-' prefix instead of 'post_tag-'. All classes are
 * passed through the filter, 'post_class' with the list of classes, followed by
 * $class parameter value, with the post ID as the last parameter.
 *
 * @since 2.7.0
 * @since 4.2.0 Custom taxonomy classes were added.
 *
 * @param string|array $class   One or more classes to add to the class list.
 * @param int|WP_Post  $post_id Optional. Post ID or post object.
 * @return array Array of classes.
 */
function get_post_class($class = '', $post_id = null)
{
    $post = get_post($post_id);
    $classes = array();
    if ($class) {
        if (!is_array($class)) {
            $class = preg_split('#\s+#', $class);
        }
        $classes = array_map('esc_attr', $class);
    }
    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 requires password
    if (post_password_required($post->ID)) {
        $classes[] = 'post-password-required';
        // Post thumbnails
    } elseif (!is_attachment($post) && current_theme_supports('post-thumbnails') && has_post_thumbnail($post->ID)) {
        $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));
    foreach ((array) $taxonomies as $taxonomy) {
        if (is_object_in_taxonomy($post->post_type, $taxonomy)) {
            foreach ((array) get_the_terms($post->ID, $taxonomy) as $term) {
                if (empty($term->slug)) {
                    continue;
                }
                $term_class = sanitize_html_class($term->slug, $term->term_id);
                if (is_numeric($term_class) || !trim($term_class, '-')) {
                    $term_class = $term->term_id;
                }
                // 'post_tag' uses the 'tag' prefix for backward compatibility.
                if ('post_tag' == $taxonomy) {
                    $classes[] = 'tag-' . $term_class;
                } else {
                    $classes[] = sanitize_html_class($taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id);
                }
            }
        }
    }
    $classes = array_map('esc_attr', $classes);
    /**
     * Filter the list of CSS classes for the current post.
     *
     * @since 2.7.0
     *
     * @param array  $classes An array of post classes.
     * @param string $class   A comma-separated list of additional classes added to the post.
     * @param int    $post_id The post ID.
     */
    $classes = apply_filters('post_class', $classes, $class, $post->ID);
    return array_unique($classes);
}

WordPress Version: 4.0

/**
 * Retrieve the classes for the post div as an array.
 *
 * The class names are many. If the post is a sticky, then the 'sticky'
 * class name. The class 'hentry' is always added to each post. If the post has a
 * post thumbnail, 'has-post-thumbnail' is added as a class. For each
 * category, the class will be added with 'category-' with category slug is
 * added. The tags are the same way as the categories with 'tag-' before the tag
 * slug. All classes are passed through the filter, 'post_class' with the list
 * of classes, followed by $class parameter value, with the post ID as the last
 * parameter.
 *
 * @since 2.7.0
 *
 * @param string|array $class One or more classes to add to the class list.
 * @param int|WP_Post $post_id Optional. Post ID or post object.
 * @return array Array of classes.
 */
function get_post_class($class = '', $post_id = null)
{
    $post = get_post($post_id);
    $classes = array();
    if (empty($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 requires password
    if (post_password_required($post->ID)) {
        $classes[] = 'post-password-required';
        // Post thumbnails
    } elseif (!is_attachment($post) && current_theme_supports('post-thumbnails') && has_post_thumbnail($post->ID)) {
        $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';
    // Categories
    if (is_object_in_taxonomy($post->post_type, 'category')) {
        foreach ((array) get_the_category($post->ID) as $cat) {
            if (empty($cat->slug)) {
                continue;
            }
            $classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->term_id);
        }
    }
    // Tags
    if (is_object_in_taxonomy($post->post_type, 'post_tag')) {
        foreach ((array) get_the_tags($post->ID) as $tag) {
            if (empty($tag->slug)) {
                continue;
            }
            $classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id);
        }
    }
    if (!empty($class)) {
        if (!is_array($class)) {
            $class = preg_split('#\s+#', $class);
        }
        $classes = array_merge($classes, $class);
    }
    $classes = array_map('esc_attr', $classes);
    /**
     * Filter the list of CSS classes for the current post.
     *
     * @since 2.7.0
     *
     * @param array  $classes An array of post classes.
     * @param string $class   A comma-separated list of additional classes added to the post.
     * @param int    $post_id The post ID.
     */
    $classes = apply_filters('post_class', $classes, $class, $post->ID);
    return array_unique($classes);
}

WordPress Version: 3.9

/**
 * Retrieve the classes for the post div as an array.
 *
 * The class names are many. If the post is a sticky, then the 'sticky'
 * class name. The class 'hentry' is always added to each post. If the post has a
 * post thumbnail, 'has-post-thumbnail' is added as a class. For each
 * category, the class will be added with 'category-' with category slug is
 * added. The tags are the same way as the categories with 'tag-' before the tag
 * slug. All classes are passed through the filter, 'post_class' with the list
 * of classes, followed by $class parameter value, with the post ID as the last
 * parameter.
 *
 * @since 2.7.0
 *
 * @param string|array $class One or more classes to add to the class list.
 * @param int|WP_Post $post_id Optional. Post ID or post object.
 * @return array Array of classes.
 */
function get_post_class($class = '', $post_id = null)
{
    $post = get_post($post_id);
    $classes = array();
    if (empty($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 requires password
    if (post_password_required($post->ID)) {
        $classes[] = 'post-password-required';
        // Post thumbnails
    } elseif (!is_attachment($post) && current_theme_supports('post-thumbnails') && has_post_thumbnail($post->ID)) {
        $classes[] = 'has-post-thumbnail';
    }
    // sticky for Sticky Posts
    if (is_sticky($post->ID) && is_home() && !is_paged()) {
        $classes[] = 'sticky';
    }
    // hentry for hAtom compliance
    $classes[] = 'hentry';
    // Categories
    if (is_object_in_taxonomy($post->post_type, 'category')) {
        foreach ((array) get_the_category($post->ID) as $cat) {
            if (empty($cat->slug)) {
                continue;
            }
            $classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->term_id);
        }
    }
    // Tags
    if (is_object_in_taxonomy($post->post_type, 'post_tag')) {
        foreach ((array) get_the_tags($post->ID) as $tag) {
            if (empty($tag->slug)) {
                continue;
            }
            $classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id);
        }
    }
    if (!empty($class)) {
        if (!is_array($class)) {
            $class = preg_split('#\s+#', $class);
        }
        $classes = array_merge($classes, $class);
    }
    $classes = array_map('esc_attr', $classes);
    /**
     * Filter the list of CSS classes for the current post.
     *
     * @since 2.7.0
     *
     * @param array  $classes An array of post classes.
     * @param string $class   A comma-separated list of additional classes added to the post.
     * @param int    $post_id The post ID.
     */
    return apply_filters('post_class', $classes, $class, $post->ID);
}

WordPress Version: 3.7

/**
 * Retrieve the classes for the post div as an array.
 *
 * The class names are add are many. If the post is a sticky, then the 'sticky'
 * class name. The class 'hentry' is always added to each post. For each
 * category, the class will be added with 'category-' with category slug is
 * added. The tags are the same way as the categories with 'tag-' before the tag
 * slug. All classes are passed through the filter, 'post_class' with the list
 * of classes, followed by $class parameter value, with the post ID as the last
 * parameter.
 *
 * @since 2.7.0
 *
 * @param string|array $class One or more classes to add to the class list.
 * @param int $post_id An optional post ID.
 * @return array Array of classes.
 */
function get_post_class($class = '', $post_id = null)
{
    $post = get_post($post_id);
    $classes = array();
    if (empty($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 requires password
    if (post_password_required($post->ID)) {
        $classes[] = 'post-password-required';
    }
    // sticky for Sticky Posts
    if (is_sticky($post->ID) && is_home() && !is_paged()) {
        $classes[] = 'sticky';
    }
    // hentry for hAtom compliance
    $classes[] = 'hentry';
    // Categories
    if (is_object_in_taxonomy($post->post_type, 'category')) {
        foreach ((array) get_the_category($post->ID) as $cat) {
            if (empty($cat->slug)) {
                continue;
            }
            $classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->term_id);
        }
    }
    // Tags
    if (is_object_in_taxonomy($post->post_type, 'post_tag')) {
        foreach ((array) get_the_tags($post->ID) as $tag) {
            if (empty($tag->slug)) {
                continue;
            }
            $classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id);
        }
    }
    if (!empty($class)) {
        if (!is_array($class)) {
            $class = preg_split('#\s+#', $class);
        }
        $classes = array_merge($classes, $class);
    }
    $classes = array_map('esc_attr', $classes);
    return apply_filters('post_class', $classes, $class, $post->ID);
}