/msw/dev/wp-includes/post.php
PHP | 4643 lines | 2248 code | 653 blank | 1742 comment | 652 complexity | 929b5cb882b435c843ad604799b0828f MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * Post functions and post utility function.
- *
- * @package WordPress
- * @subpackage Post
- * @since 1.5.0
- */
- //
- // Post Type Registration
- //
- /**
- * Creates the initial post types when 'init' action is fired.
- */
- function create_initial_post_types() {
- register_post_type( 'post', array(
- 'label' => __( 'Posts' ),
- 'singular_label' => __( 'Post' ),
- 'public' => true,
- 'show_ui' => false,
- '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
- '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
- 'capability_type' => 'post',
- 'hierarchical' => false,
- 'rewrite' => false,
- 'query_var' => false,
- 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions' ),
- ) );
- register_post_type( 'page', array(
- 'label' => __( 'Pages' ),
- 'singular_label' => __( 'Page' ),
- 'public' => true,
- 'show_ui' => false,
- '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
- '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
- 'capability_type' => 'page',
- 'hierarchical' => true,
- 'rewrite' => false,
- 'query_var' => false,
- 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
- ) );
- register_post_type( 'attachment', array(
- 'label' => __( 'Media' ),
- 'public' => true,
- 'show_ui' => false,
- '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
- '_edit_link' => 'media.php?attachment_id=%d', /* internal use only. don't use this when registering your own post type. */
- 'capability_type' => 'post',
- 'hierarchical' => false,
- 'rewrite' => false,
- 'query_var' => false,
- 'can_export' => false,
- ) );
- register_post_type( 'revision', array(
- 'label' => __( 'Revisions' ),
- 'singular_label' => __( 'Revision' ),
- 'public' => false,
- '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
- '_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */
- 'capability_type' => 'post',
- 'hierarchical' => false,
- 'rewrite' => false,
- 'query_var' => false,
- ) );
- register_post_type( 'nav_menu_item', array(
- 'label' => __( 'Navigation Menu Items' ),
- 'singular_label' => __( 'Navigation Menu Item' ),
- 'public' => false,
- 'show_ui' => false,
- '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
- 'capability_type' => 'post',
- 'hierarchical' => false,
- 'rewrite' => false,
- 'query_var' => false,
- ) );
- register_post_status( 'publish', array(
- 'label' => _x( 'Published', 'post' ),
- 'public' => true,
- '_builtin' => true, /* internal use only. */
- 'label_count' => _n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' ),
- ) );
- register_post_status( 'future', array(
- 'label' => _x( 'Scheduled', 'post' ),
- 'protected' => true,
- '_builtin' => true, /* internal use only. */
- 'label_count' => _n_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' ),
- ) );
- register_post_status( 'draft', array(
- 'label' => _x( 'Draft', 'post' ),
- 'protected' => true,
- '_builtin' => true, /* internal use only. */
- 'label_count' => _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ),
- ) );
- register_post_status( 'pending', array(
- 'label' => _x( 'Pending', 'post' ),
- 'protected' => true,
- '_builtin' => true, /* internal use only. */
- 'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>' ),
- ) );
- register_post_status( 'private', array(
- 'label' => _x( 'Private', 'post' ),
- 'private' => true,
- '_builtin' => true, /* internal use only. */
- 'label_count' => _n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' ),
- ) );
- register_post_status( 'trash', array(
- 'label' => _x( 'Trash', 'post' ),
- 'internal' => true,
- '_builtin' => true, /* internal use only. */
- 'label_count' => _n_noop( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>' ),
- 'show_in_admin_status_list' => true,
- ) );
- register_post_status( 'auto-draft', array(
- 'label' => 'auto-draft',
- 'internal' => true,
- '_builtin' => true, /* internal use only. */
- ) );
- register_post_status( 'inherit', array(
- 'label' => 'inherit',
- 'internal' => true,
- '_builtin' => true, /* internal use only. */
- 'exclude_from_search' => false,
- ) );
- }
- add_action( 'init', 'create_initial_post_types', 0 ); // highest priority
- /**
- * Retrieve attached file path based on attachment ID.
- *
- * You can optionally send it through the 'get_attached_file' filter, but by
- * default it will just return the file path unfiltered.
- *
- * The function works by getting the single post meta name, named
- * '_wp_attached_file' and returning it. This is a convenience function to
- * prevent looking up the meta name and provide a mechanism for sending the
- * attached filename through a filter.
- *
- * @since 2.0.0
- * @uses apply_filters() Calls 'get_attached_file' on file path and attachment ID.
- *
- * @param int $attachment_id Attachment ID.
- * @param bool $unfiltered Whether to apply filters.
- * @return string The file path to the attached file.
- */
- function get_attached_file( $attachment_id, $unfiltered = false ) {
- $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
- // If the file is relative, prepend upload dir
- if ( 0 !== strpos($file, '/') && !preg_match('|^.:\\\|', $file) && ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) )
- $file = $uploads['basedir'] . "/$file";
- if ( $unfiltered )
- return $file;
- return apply_filters( 'get_attached_file', $file, $attachment_id );
- }
- /**
- * Update attachment file path based on attachment ID.
- *
- * Used to update the file path of the attachment, which uses post meta name
- * '_wp_attached_file' to store the path of the attachment.
- *
- * @since 2.1.0
- * @uses apply_filters() Calls 'update_attached_file' on file path and attachment ID.
- *
- * @param int $attachment_id Attachment ID
- * @param string $file File path for the attachment
- * @return bool False on failure, true on success.
- */
- function update_attached_file( $attachment_id, $file ) {
- if ( !get_post( $attachment_id ) )
- return false;
- $file = apply_filters( 'update_attached_file', $file, $attachment_id );
- $file = _wp_relative_upload_path($file);
- return update_post_meta( $attachment_id, '_wp_attached_file', $file );
- }
- /**
- * Return relative path to an uploaded file.
- *
- * The path is relative to the current upload dir.
- *
- * @since 2.9.0
- * @uses apply_filters() Calls '_wp_relative_upload_path' on file path.
- *
- * @param string $path Full path to the file
- * @return string relative path on success, unchanged path on failure.
- */
- function _wp_relative_upload_path( $path ) {
- $new_path = $path;
- if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) {
- if ( 0 === strpos($new_path, $uploads['basedir']) ) {
- $new_path = str_replace($uploads['basedir'], '', $new_path);
- $new_path = ltrim($new_path, '/');
- }
- }
- return apply_filters( '_wp_relative_upload_path', $new_path, $path );
- }
- /**
- * Retrieve all children of the post parent ID.
- *
- * Normally, without any enhancements, the children would apply to pages. In the
- * context of the inner workings of WordPress, pages, posts, and attachments
- * share the same table, so therefore the functionality could apply to any one
- * of them. It is then noted that while this function does not work on posts, it
- * does not mean that it won't work on posts. It is recommended that you know
- * what context you wish to retrieve the children of.
- *
- * Attachments may also be made the child of a post, so if that is an accurate
- * statement (which needs to be verified), it would then be possible to get
- * all of the attachments for a post. Attachments have since changed since
- * version 2.5, so this is most likely unaccurate, but serves generally as an
- * example of what is possible.
- *
- * The arguments listed as defaults are for this function and also of the
- * {@link get_posts()} function. The arguments are combined with the
- * get_children defaults and are then passed to the {@link get_posts()}
- * function, which accepts additional arguments. You can replace the defaults in
- * this function, listed below and the additional arguments listed in the
- * {@link get_posts()} function.
- *
- * The 'post_parent' is the most important argument and important attention
- * needs to be paid to the $args parameter. If you pass either an object or an
- * integer (number), then just the 'post_parent' is grabbed and everything else
- * is lost. If you don't specify any arguments, then it is assumed that you are
- * in The Loop and the post parent will be grabbed for from the current post.
- *
- * The 'post_parent' argument is the ID to get the children. The 'numberposts'
- * is the amount of posts to retrieve that has a default of '-1', which is
- * used to get all of the posts. Giving a number higher than 0 will only
- * retrieve that amount of posts.
- *
- * The 'post_type' and 'post_status' arguments can be used to choose what
- * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress
- * post types are 'post', 'pages', and 'attachments'. The 'post_status'
- * argument will accept any post status within the write administration panels.
- *
- * @see get_posts() Has additional arguments that can be replaced.
- * @internal Claims made in the long description might be inaccurate.
- *
- * @since 2.0.0
- *
- * @param mixed $args Optional. User defined arguments for replacing the defaults.
- * @param string $output Optional. Constant for return type, either OBJECT (default), ARRAY_A, ARRAY_N.
- * @return array|bool False on failure and the type will be determined by $output parameter.
- */
- function &get_children($args = '', $output = OBJECT) {
- $kids = array();
- if ( empty( $args ) ) {
- if ( isset( $GLOBALS['post'] ) ) {
- $args = array('post_parent' => (int) $GLOBALS['post']->post_parent );
- } else {
- return $kids;
- }
- } elseif ( is_object( $args ) ) {
- $args = array('post_parent' => (int) $args->post_parent );
- } elseif ( is_numeric( $args ) ) {
- $args = array('post_parent' => (int) $args);
- }
- $defaults = array(
- 'numberposts' => -1, 'post_type' => 'any',
- 'post_status' => 'any', 'post_parent' => 0,
- );
- $r = wp_parse_args( $args, $defaults );
- $children = get_posts( $r );
- if ( !$children )
- return $kids;
- update_post_cache($children);
- foreach ( $children as $key => $child )
- $kids[$child->ID] =& $children[$key];
- if ( $output == OBJECT ) {
- return $kids;
- } elseif ( $output == ARRAY_A ) {
- foreach ( (array) $kids as $kid )
- $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
- return $weeuns;
- } elseif ( $output == ARRAY_N ) {
- foreach ( (array) $kids as $kid )
- $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
- return $babes;
- } else {
- return $kids;
- }
- }
- /**
- * Get extended entry info (<!--more-->).
- *
- * There should not be any space after the second dash and before the word
- * 'more'. There can be text or space(s) after the word 'more', but won't be
- * referenced.
- *
- * The returned array has 'main' and 'extended' keys. Main has the text before
- * the <code><!--more--></code>. The 'extended' key has the content after the
- * <code><!--more--></code> comment.
- *
- * @since 1.0.0
- *
- * @param string $post Post content.
- * @return array Post before ('main') and after ('extended').
- */
- function get_extended($post) {
- //Match the new style more links
- if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) {
- list($main, $extended) = explode($matches[0], $post, 2);
- } else {
- $main = $post;
- $extended = '';
- }
- // Strip leading and trailing whitespace
- $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
- $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
- return array('main' => $main, 'extended' => $extended);
- }
- /**
- * Retrieves post data given a post ID or post object.
- *
- * See {@link sanitize_post()} for optional $filter values. Also, the parameter
- * $post, must be given as a variable, since it is passed by reference.
- *
- * @since 1.5.1
- * @uses $wpdb
- * @link http://codex.wordpress.org/Function_Reference/get_post
- *
- * @param int|object $post Post ID or post object.
- * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
- * @param string $filter Optional, default is raw.
- * @return mixed Post data
- */
- function &get_post(&$post, $output = OBJECT, $filter = 'raw') {
- global $wpdb;
- $null = null;
- if ( empty($post) ) {
- if ( isset($GLOBALS['post']) )
- $_post = & $GLOBALS['post'];
- else
- return $null;
- } elseif ( is_object($post) && empty($post->filter) ) {
- _get_post_ancestors($post);
- $_post = sanitize_post($post, 'raw');
- wp_cache_add($post->ID, $_post, 'posts');
- } else {
- if ( is_object($post) )
- $post = $post->ID;
- $post = (int) $post;
- if ( ! $_post = wp_cache_get($post, 'posts') ) {
- $_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post));
- if ( ! $_post )
- return $null;
- _get_post_ancestors($_post);
- $_post = sanitize_post($_post, 'raw');
- wp_cache_add($_post->ID, $_post, 'posts');
- }
- }
- if ($filter != 'raw')
- $_post = sanitize_post($_post, $filter);
- if ( $output == OBJECT ) {
- return $_post;
- } elseif ( $output == ARRAY_A ) {
- $__post = get_object_vars($_post);
- return $__post;
- } elseif ( $output == ARRAY_N ) {
- $__post = array_values(get_object_vars($_post));
- return $__post;
- } else {
- return $_post;
- }
- }
- /**
- * Retrieve ancestors of a post.
- *
- * @since 2.5.0
- *
- * @param int|object $post Post ID or post object
- * @return array Ancestor IDs or empty array if none are found.
- */
- function get_post_ancestors($post) {
- $post = get_post($post);
- if ( !empty($post->ancestors) )
- return $post->ancestors;
- return array();
- }
- /**
- * Retrieve data from a post field based on Post ID.
- *
- * Examples of the post field will be, 'post_type', 'post_status', 'content',
- * etc and based off of the post object property or key names.
- *
- * The context values are based off of the taxonomy filter functions and
- * supported values are found within those functions.
- *
- * @since 2.3.0
- * @uses sanitize_post_field() See for possible $context values.
- *
- * @param string $field Post field name
- * @param id $post Post ID
- * @param string $context Optional. How to filter the field. Default is display.
- * @return WP_Error|string Value in post field or WP_Error on failure
- */
- function get_post_field( $field, $post, $context = 'display' ) {
- $post = (int) $post;
- $post = get_post( $post );
- if ( is_wp_error($post) )
- return $post;
- if ( !is_object($post) )
- return '';
- if ( !isset($post->$field) )
- return '';
- return sanitize_post_field($field, $post->$field, $post->ID, $context);
- }
- /**
- * Retrieve the mime type of an attachment based on the ID.
- *
- * This function can be used with any post type, but it makes more sense with
- * attachments.
- *
- * @since 2.0.0
- *
- * @param int $ID Optional. Post ID.
- * @return bool|string False on failure or returns the mime type
- */
- function get_post_mime_type($ID = '') {
- $post = & get_post($ID);
- if ( is_object($post) )
- return $post->post_mime_type;
- return false;
- }
- /**
- * Retrieve the post status based on the Post ID.
- *
- * If the post ID is of an attachment, then the parent post status will be given
- * instead.
- *
- * @since 2.0.0
- *
- * @param int $ID Post ID
- * @return string|bool Post status or false on failure.
- */
- function get_post_status($ID = '') {
- $post = get_post($ID);
- if ( !is_object($post) )
- return false;
- // Unattached attachments are assumed to be published.
- if ( ('attachment' == $post->post_type) && ('inherit' == $post->post_status) && ( 0 == $post->post_parent) )
- return 'publish';
- if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) )
- return get_post_status($post->post_parent);
- return $post->post_status;
- }
- /**
- * Retrieve all of the WordPress supported post statuses.
- *
- * Posts have a limited set of valid status values, this provides the
- * post_status values and descriptions.
- *
- * @since 2.5.0
- *
- * @return array List of post statuses.
- */
- function get_post_statuses( ) {
- $status = array(
- 'draft' => __('Draft'),
- 'pending' => __('Pending Review'),
- 'private' => __('Private'),
- 'publish' => __('Published')
- );
- return $status;
- }
- /**
- * Retrieve all of the WordPress support page statuses.
- *
- * Pages have a limited set of valid status values, this provides the
- * post_status values and descriptions.
- *
- * @since 2.5.0
- *
- * @return array List of page statuses.
- */
- function get_page_statuses( ) {
- $status = array(
- 'draft' => __('Draft'),
- 'private' => __('Private'),
- 'publish' => __('Published')
- );
- return $status;
- }
- /**
- * Register a post type. Do not use before init.
- *
- * A simple function for creating or modifying a post status based on the
- * parameters given. The function will accept an array (second optional
- * parameter), along with a string for the post status name.
- *
- *
- * Optional $args contents:
- *
- * label - A descriptive name for the post status marked for translation. Defaults to $post_status.
- * public - Whether posts of this status should be shown in the admin UI. Defaults to true.
- * exclude_from_search - Whether to exclude posts with this post status from search results. Defaults to true.
- *
- * @package WordPress
- * @subpackage Post
- * @since 3.0.0
- * @uses $wp_post_statuses Inserts new post status object into the list
- *
- * @param string $post_status Name of the post status.
- * @param array|string $args See above description.
- */
- function register_post_status($post_status, $args = array()) {
- global $wp_post_statuses;
- if (!is_array($wp_post_statuses))
- $wp_post_statuses = array();
- // Args prefixed with an underscore are reserved for internal use.
- $defaults = array('label' => false, 'label_count' => false, 'exclude_from_search' => null, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => null, 'internal' => null, 'protected' => null, 'private' => null, 'show_in_admin_all' => null, 'publicly_queryable' => null, 'show_in_admin_status_list' => null, 'show_in_admin_all_list' => null, 'single_view_cap' => null);
- $args = wp_parse_args($args, $defaults);
- $args = (object) $args;
- $post_status = sanitize_user($post_status, true);
- $args->name = $post_status;
- if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private )
- $args->internal = true;
- if ( null === $args->public )
- $args->public = false;
- if ( null === $args->private )
- $args->private = false;
- if ( null === $args->protected )
- $args->protected = false;
- if ( null === $args->internal )
- $args->internal = false;
- if ( null === $args->publicly_queryable )
- $args->publicly_queryable = $args->public;
- if ( null === $args->exclude_from_search )
- $args->exclude_from_search = $args->internal;
- if ( null === $args->show_in_admin_all_list )
- $args->show_in_admin_all_list = !$args->internal;
- if ( null === $args->show_in_admin_status_list )
- $args->show_in_admin_status_list = !$args->internal;
- if ( null === $args->single_view_cap )
- $args->single_view_cap = $args->public ? '' : 'edit';
- if ( false === $args->label )
- $args->label = $post_status;
- if ( false === $args->label_count )
- $args->label_count = array( $args->label, $args->label );
- $wp_post_statuses[$post_status] = $args;
- return $args;
- }
- /**
- * Retrieve a post status object by name
- *
- * @package WordPress
- * @subpackage Post
- * @since 3.0.0
- * @uses $wp_post_statuses
- * @see register_post_status
- * @see get_post_statuses
- *
- * @param string $post_type The name of a registered post status
- * @return object A post status object
- */
- function get_post_status_object( $post_status ) {
- global $wp_post_statuses;
- if ( empty($wp_post_statuses[$post_status]) )
- return null;
- return $wp_post_statuses[$post_status];
- }
- /**
- * Get a list of all registered post status objects.
- *
- * @package WordPress
- * @subpackage Post
- * @since 3.0.0
- * @uses $wp_post_statuses
- * @see register_post_status
- * @see get_post_status_object
- *
- * @param array|string $args An array of key => value arguments to match against the post status objects.
- * @param string $output The type of output to return, either post status 'names' or 'objects'. 'names' is the default.
- * @param string $operator The logical operation to perform. 'or' means only one element
- * from the array needs to match; 'and' means all elements must match. The default is 'and'.
- * @return array A list of post type names or objects
- */
- function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
- global $wp_post_statuses;
- $field = ('names' == $output) ? 'name' : false;
- return wp_filter_object_list($wp_post_statuses, $args, $operator, $field);
- }
- /**
- * Whether the post type is hierarchical.
- *
- * A false return value might also mean that the post type does not exist.
- *
- * @since 3.0.0
- * @see get_post_type_object
- *
- * @param string|int|object $post Post type name, post id, or a post object.
- * @return bool true if post type is hierarchical, else false.
- */
- function is_post_type_hierarchical( $post = false ) {
- if ( is_string($post) && $is_post_type = get_post_type_object($post) )
- return $is_post_type->hierarchical;
- $ptype = get_post( $post );
- if ( $ptype && $is_post_type = get_post_type_object($ptype->post_type) )
- return $is_post_type->hierarchical;
- return false;
- }
- /**
- * Checks if a post type is registered, can also check if the current or specified post is of a post type.
- *
- * @since 3.0.0
- * @uses get_post_type()
- *
- * @param string|array $types Type or types to check. Defaults to all post types.
- * @param int $id Post ID. Defaults to current ID.
- * @return bool
- */
- function is_post_type( $types = false, $id = false ) {
- $types = ( $types === false ) ? get_post_types() : (array) $types;
- return in_array( get_post_type( $id ), $types );
- }
- /**
- * Retrieve the post type of the current post or of a given post.
- *
- * @since 2.1.0
- *
- * @uses $post The Loop current post global
- *
- * @param mixed $the_post Optional. Post object or post ID.
- * @return bool|string post type or false on failure.
- */
- function get_post_type( $the_post = false ) {
- global $post;
- if ( false === $the_post )
- $the_post = $post;
- elseif ( is_numeric($the_post) )
- $the_post = get_post($the_post);
- if ( is_object($the_post) )
- return $the_post->post_type;
- return false;
- }
- /**
- * Retrieve a post type object by name
- *
- * @package WordPress
- * @subpackage Post
- * @since 3.0.0
- * @uses $wp_post_types
- * @see register_post_type
- * @see get_post_types
- *
- * @param string $post_type The name of a registered post type
- * @return object A post type object
- */
- function get_post_type_object( $post_type ) {
- global $wp_post_types;
- if ( empty($wp_post_types[$post_type]) )
- return null;
- return $wp_post_types[$post_type];
- }
- /**
- * Get a list of all registered post type objects.
- *
- * @package WordPress
- * @subpackage Post
- * @since 2.9.0
- * @uses $wp_post_types
- * @see register_post_type
- *
- * @param array|string $args An array of key => value arguments to match against the post type objects.
- * @param string $output The type of output to return, either post type 'names' or 'objects'. 'names' is the default.
- * @param string $operator The logical operation to perform. 'or' means only one element
- * from the array needs to match; 'and' means all elements must match. The default is 'and'.
- * @return array A list of post type names or objects
- */
- function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
- global $wp_post_types;
- $field = ('names' == $output) ? 'name' : false;
- return wp_filter_object_list($wp_post_types, $args, $operator, $field);
- }
- /**
- * Register a post type. Do not use before init.
- *
- * A simple function for creating or modifying a post type based on the
- * parameters given. The function will accept an array (second optional
- * parameter), along with a string for the post type name.
- *
- *
- * Optional $args contents:
- *
- * label - A (plural) descriptive name for the post type marked for translation. Defaults to $post_type.
- * singular_label - A (singular) descriptive name for the post type marked for translation. Defaults to $label.
- * description - A short descriptive summary of what the post type is. Defaults to blank.
- * public - Whether posts of this type should be shown in the admin UI. Defaults to false.
- * exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true if the type is not public, false if the type is public.
- * publicly_queryable - Whether post_type queries can be performed from the front page. Defaults to whatever public is set as.
- * show_ui - Whether to generate a default UI for managing this post type. Defaults to true if the type is public, false if the type is not public.
- * menu_position - The position in the menu order the post type should appear. Defaults to the bottom.
- * menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
- * inherit_type - The post type from which to inherit the edit link and capability type. Defaults to none.
- * capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
- * edit_cap - The capability that controls editing a particular object of this post type. Defaults to "edit_$capability_type" (edit_post).
- * edit_type_cap - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts).
- * edit_others_cap - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s" (edit_others_posts).
- * publish_others_cap - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts).
- * read_cap - The capability that controls reading a particular object of this post type. Defaults to "read_$capability_type" (read_post).
- * delete_cap - The capability that controls deleting a particular object of this post type. Defaults to "delete_$capability_type" (delete_post).
- * hierarchical - Whether the post type is hierarchical. Defaults to false.
- * supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none.
- * register_meta_box_cb - Provide a callback function that will be called when setting up the meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback.
- * taxonomies - An array of taxonomy identifiers that will be registered for the post type. Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type().
- *
- * @package WordPress
- * @subpackage Post
- * @since 2.9.0
- * @uses $wp_post_types Inserts new post type object into the list
- *
- * @param string $post_type Name of the post type.
- * @param array|string $args See above description.
- */
- function register_post_type($post_type, $args = array()) {
- global $wp_post_types, $wp_rewrite, $wp;
- if ( !is_array($wp_post_types) )
- $wp_post_types = array();
- // Args prefixed with an underscore are reserved for internal use.
- $defaults = array('label' => false, 'singular_label' => false, 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => false, 'rewrite' => true, 'query_var' => true, 'supports' => array(), 'register_meta_box_cb' => null, 'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null, 'permalink_epmask' => EP_PERMALINK, 'can_export' => true );
- $args = wp_parse_args($args, $defaults);
- $args = (object) $args;
- $post_type = sanitize_user($post_type, true);
- $args->name = $post_type;
- // If not set, default to the setting for public.
- if ( null === $args->publicly_queryable )
- $args->publicly_queryable = $args->public;
- // If not set, default to the setting for public.
- if ( null === $args->show_ui )
- $args->show_ui = $args->public;
- // If not set, default to true if not public, false if public.
- if ( null === $args->exclude_from_search )
- $args->exclude_from_search = !$args->public;
- if ( false === $args->label )
- $args->label = $post_type;
- if ( false === $args->singular_label )
- $args->singular_label = $args->label;
- if ( empty($args->capability_type) )
- $args->capability_type = 'post';
- if ( empty($args->edit_cap) )
- $args->edit_cap = 'edit_' . $args->capability_type;
- if ( empty($args->edit_type_cap) )
- $args->edit_type_cap = 'edit_' . $args->capability_type . 's';
- if ( empty($args->edit_others_cap) )
- $args->edit_others_cap = 'edit_others_' . $args->capability_type . 's';
- if ( empty($args->publish_cap) )
- $args->publish_cap = 'publish_' . $args->capability_type . 's';
- if ( empty($args->read_cap) )
- $args->read_cap = 'read_' . $args->capability_type;
- if ( empty($args->read_private_cap) )
- $args->read_private_cap = 'read_private_' . $args->capability_type . 's';
- if ( empty($args->delete_cap) )
- $args->delete_cap = 'delete_' . $args->capability_type;
- if ( ! empty($args->supports) ) {
- add_post_type_support($post_type, $args->supports);
- unset($args->supports);
- } else {
- // Add default features
- add_post_type_support($post_type, array('title', 'editor'));
- }
- if ( false !== $args->query_var && !empty($wp) ) {
- if ( true === $args->query_var )
- $args->query_var = $post_type;
- $args->query_var = sanitize_title_with_dashes($args->query_var);
- $wp->add_query_var($args->query_var);
- }
- if ( false !== $args->rewrite && '' != get_option('permalink_structure') ) {
- if ( !is_array($args->rewrite) )
- $args->rewrite = array();
- if ( !isset($args->rewrite['slug']) )
- $args->rewrite['slug'] = $post_type;
- if ( !isset($args->rewrite['with_front']) )
- $args->rewrite['with_front'] = true;
- if ( $args->hierarchical )
- $wp_rewrite->add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
- else
- $wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
- $wp_rewrite->add_permastruct($post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask);
- }
- if ( $args->register_meta_box_cb )
- add_action('add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1);
- $wp_post_types[$post_type] = $args;
- add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 );
- foreach ( $args->taxonomies as $taxonomy ) {
- register_taxonomy_for_object_type( $taxonomy, $post_type );
- }
- return $args;
- }
- /**
- * Register support of certain features for a post type.
- *
- * All features are directly associated with a functional area of the edit screen, such as the
- * editor or a meta box: 'title', 'editor', 'comments', 'revisions', 'trackbacks', 'author',
- * 'excerpt', 'page-attributes', 'thumbnail', and 'custom-fields'.
- *
- * Additionally, the 'revisions' feature dictates whether the post type will store revisions,
- * and the 'comments' feature dicates whether the comments count will show on the edit screen.
- *
- * @since 3.0.0
- * @param string $post_type The post type for which to add the feature
- * @param string|array $feature the feature being added, can be an array of feature strings or a single string
- */
- function add_post_type_support( $post_type, $feature ) {
- global $_wp_post_type_features;
- $features = (array) $feature;
- foreach ($features as $feature) {
- if ( func_num_args() == 2 )
- $_wp_post_type_features[$post_type][$feature] = true;
- else
- $_wp_post_type_features[$post_type][$feature] = array_slice( func_get_args(), 2 );
- }
- }
- /**
- * Remove support for a feature from a post type.
- *
- * @since 3.0.0
- * @param string $post_type The post type for which to remove the feature
- * @param string $feature The feature being removed
- */
- function remove_post_type_support( $post_type, $feature ) {
- global $_wp_post_type_features;
- if ( !isset($_wp_post_type_features[$post_type]) )
- return;
- if ( isset($_wp_post_type_features[$post_type][$feature]) )
- unset($_wp_post_type_features[$post_type][$feature]);
- }
- /**
- * Checks a post type's support for a given feature
- *
- * @since 3.0.0
- * @param string $post_type The post type being checked
- * @param string $feature the feature being checked
- * @return boolean
- */
- function post_type_supports( $post_type, $feature ) {
- global $_wp_post_type_features;
- if ( !isset( $_wp_post_type_features[$post_type][$feature] ) )
- return false;
- // If no args passed then no extra checks need be performed
- if ( func_num_args() <= 2 )
- return true;
- // @todo Allow pluggable arg checking
- //$args = array_slice( func_get_args(), 2 );
- return true;
- }
- /**
- * Updates the post type for the post ID.
- *
- * The page or post cache will be cleaned for the post ID.
- *
- * @since 2.5.0
- *
- * @uses $wpdb
- *
- * @param int $post_id Post ID to change post type. Not actually optional.
- * @param string $post_type Optional, default is post. Supported values are 'post' or 'page' to
- * name a few.
- * @return int Amount of rows changed. Should be 1 for success and 0 for failure.
- */
- function set_post_type( $post_id = 0, $post_type = 'post' ) {
- global $wpdb;
- $post_type = sanitize_post_field('post_type', $post_type, $post_id, 'db');
- $return = $wpdb->update($wpdb->posts, array('post_type' => $post_type), array('ID' => $post_id) );
- if ( 'page' == $post_type )
- clean_page_cache($post_id);
- else
- clean_post_cache($post_id);
- return $return;
- }
- /**
- * Retrieve list of latest posts or posts matching criteria.
- *
- * The defaults are as follows:
- * 'numberposts' - Default is 5. Total number of posts to retrieve.
- * 'offset' - Default is 0. See {@link WP_Query::query()} for more.
- * 'category' - What category to pull the posts from.
- * 'orderby' - Default is 'post_date'. How to order the posts.
- * 'order' - Default is 'DESC'. The order to retrieve the posts.
- * 'include' - See {@link WP_Query::query()} for more.
- * 'exclude' - See {@link WP_Query::query()} for more.
- * 'meta_key' - See {@link WP_Query::query()} for more.
- * 'meta_value' - See {@link WP_Query::query()} for more.
- * 'post_type' - Default is 'post'. Can be 'page', or 'attachment' to name a few.
- * 'post_parent' - The parent of the post or post type.
- * 'post_status' - Default is 'published'. Post status to retrieve.
- *
- * @since 1.2.0
- * @uses $wpdb
- * @uses WP_Query::query() See for more default arguments and information.
- * @link http://codex.wordpress.org/Template_Tags/get_posts
- *
- * @param array $args Optional. Overrides defaults.
- * @return array List of posts.
- */
- function get_posts($args = null) {
- $defaults = array(
- 'numberposts' => 5, 'offset' => 0,
- 'category' => 0, 'orderby' => 'post_date',
- 'order' => 'DESC', 'include' => array(),
- 'exclude' => array(), 'meta_key' => '',
- 'meta_value' =>'', 'post_type' => 'post',
- 'suppress_filters' => true
- );
- $r = wp_parse_args( $args, $defaults );
- if ( empty( $r['post_status'] ) )
- $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
- if ( ! empty($r['numberposts']) )
- $r['posts_per_page'] = $r['numberposts'];
- if ( ! empty($r['category']) )
- $r['cat'] = $r['category'];
- if ( ! empty($r['include']) ) {
- $incposts = wp_parse_id_list( $r['include'] );
- $r['posts_per_page'] = count($incposts); // only the number of posts included
- $r['post__in'] = $incposts;
- } elseif ( ! empty($r['exclude']) )
- $r['post__not_in'] = wp_parse_id_list( $r['exclude'] );
- $r['caller_get_posts'] = true;
- $get_posts = new WP_Query;
- return $get_posts->query($r);
- }
- //
- // Post meta functions
- //
- /**
- * Add meta data field to a post.
- *
- * Post meta data is called "Custom Fields" on the Administration Panels.
- *
- * @since 1.5.0
- * @uses $wpdb
- * @link http://codex.wordpress.org/Function_Reference/add_post_meta
- *
- * @param int $post_id Post ID.
- * @param string $key Metadata name.
- * @param mixed $value Metadata value.
- * @param bool $unique Optional, default is false. Whether the same key should not be added.
- * @return bool False for failure. True for success.
- */
- function add_post_meta($post_id, $meta_key, $meta_value, $unique = false) {
- // make sure meta is added to the post, not a revision
- if ( $the_post = wp_is_post_revision($post_id) )
- $post_id = $the_post;
- return add_metadata('post', $post_id, $meta_key, $meta_value, $unique);
- }
- /**
- * Remove metadata matching criteria from a post.
- *
- * You can match based on the key, or key and value. Removing based on key and
- * value, will keep from removing duplicate metadata with the same key. It also
- * allows removing all metadata matching key, if needed.
- *
- * @since 1.5.0
- * @uses $wpdb
- * @link http://codex.wordpress.org/Function_Reference/delete_post_meta
- *
- * @param int $post_id post ID
- * @param string $meta_key Metadata name.
- * @param mixed $meta_value Optional. Metadata value.
- * @return bool False for failure. True for success.
- */
- function delete_post_meta($post_id, $meta_key, $meta_value = '') {
- // make sure meta is added to the post, not a revision
- if ( $the_post = wp_is_post_revision($post_id) )
- $post_id = $the_post;
- return delete_metadata('post', $post_id, $meta_key, $meta_value);
- }
- /**
- * Retrieve post meta field for a post.
- *
- * @since 1.5.0
- * @uses $wpdb
- * @link http://codex.wordpress.org/Function_Reference/get_post_meta
- *
- * @param int $post_id Post ID.
- * @param string $key The meta key to retrieve.
- * @param bool $single Whether to return a single value.
- * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
- * is true.
- */
- function get_post_meta($post_id, $key, $single = false) {
- return get_metadata('post', $post_id, $key, $single);
- }
- /**
- * Update post meta field based on post ID.
- *
- * Use the $prev_value parameter to differentiate between meta fields with the
- * same key and post ID.
- *
- * If the meta field for the post does not exist, it will be added.
- *
- * @since 1.5
- * @uses $wpdb
- * @link http://codex.wordpress.org/Function_Reference/update_post_meta
- *
- * @param int $post_id Post ID.
- * @param string $key Metadata key.
- * @param mixed $value Metadata value.
- * @param mixed $prev_value Optional. Previous value to check before removing.
- * @return bool False on failure, true if success.
- */
- function update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '') {
- // make sure meta is added to the post, not a revision
- if ( $the_post = wp_is_post_revision($post_id) )
- $post_id = $the_post;
- return update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value);
- }
- /**
- * Delete everything from post meta matching meta key.
- *
- * @since 2.3.0
- * @uses $wpdb
- *
- * @param string $post_meta_key Key to search for when deleting.
- * @return bool Whether the post meta key was deleted from the database
- */
- function delete_post_meta_by_key($post_meta_key) {
- if ( !$post_meta_key )
- return false;
- global $wpdb;
- $post_ids = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key));
- if ( $post_ids ) {
- $postmetaids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key ) );
- $in = implode( ',', array_fill(1, count($postmetaids), '%d'));
- do_action( 'delete_postmeta', $postmetaids );
- $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id IN($in)", $postmetaids ));
- do_action( 'deleted_postmeta', $postmetaids );
- foreach ( $post_ids as $post_id )
- wp_cache_delete($post_id, 'post_meta');
- return true;
- }
- return false;
- }
- /**
- * Retrieve post meta fields, based on post ID.
- *
- * The post meta fields are retrieved from the cache, so the function is
- * optimized to be called more than once. It also applies to the functions, that
- * use this function.
- *
- * @since 1.2.0
- * @link http://codex.wordpress.org/Function_Reference/get_post_custom
- *
- * @uses $id Current Loop Post ID
- *
- * @param int $post_id post ID
- * @return array
- */
- function get_post_custom($post_id = 0) {
- global $id;
- if ( !$post_id )
- $post_id = (int) $id;
- $post_id = (int) $post_id;
- if ( ! wp_cache_get($post_id, 'post_meta') )
- update_postmeta_cache($post_id);
- return wp_cache_get($post_id, 'post_meta');
- }
- /**
- * Retrieve meta field names for a post.
- *
- * If there are no meta fields, then nothing (null) will be returned.
- *
- * @since 1.2.0
- * @link http://codex.wordpress.org/Function_Reference/get_post_custom_keys
- *
- * @param int $post_id post ID
- * @return array|null Either array of the keys, or null if keys could not be retrieved.
- */
- function get_post_custom_keys( $post_id = 0 ) {
- $custom = get_post_custom( $post_id );
- if ( !is_array($custom) )
- return;
- if ( $keys = array_keys($custom) )
- return $keys;
- }
- /**
- * Retrieve values for a custom post field.
- *
- * The parameters must not be considered optional. All of the post meta fields
- * will be retrieved and only the meta field key values returned.
- *
- * @since 1.2.0
- * @link http://codex.wordpress.org/Function_Reference/get_post_custom_values
- *
- * @param string $key Meta field key.
- * @param int $post_id Post ID
- * @return array Meta field values.
- */
- function get_post_custom_values( $key = '', $post_id = 0 ) {
- if ( !$key )
- return null;
- $custom = get_post_custom($post_id);
- return isset($custom[$key]) ? $custom[$key] : null;
- }
- /**
- * Check if post is sticky.
- *
- * Sticky posts should remain at the top of The Loop. If the post ID is not
- * given, then The Loop ID for the current post will be used.
- *
- * @since 2.7.0
- *
- * @param int $post_id Optional. Post ID.
- * @return bool Whether post is sticky.
- */
- function is_sticky($post_id = null) {
- global $id;
- $post_id = absint($post_id);
- if ( !$post_id )
- $post_id = absint($id);
- $stickies = get_option('sticky_posts');
- if ( !is_array($stickies) )
- return false;
- if ( in_array($post_id, $stickies) )
- return true;
- return false;
- }
- /**
- * Sanitize every post field.
- *
- * If the context is 'raw', then the post object or array will get minimal santization of the int fields.
- *
- * @since 2.3.0
- * @uses sanitize_post_field() Used to sanitize the fields.
- *
- * @param object|array $post The Post Object or Array
- * @param string $context Optional, default is 'display'. How to sanitize post fields.
- * @return object|array The now sanitized Post Object or Array (will be the same type as $post)
- */
- function sanitize_post($post, $context = 'display') {
- if ( is_object($post) ) {
- // Check if post already filtered for this context
- if ( isset($post->filter) && $context == $post->filter )
- return $post;
- if ( !isset($post->ID) )
- $post->ID = 0;
- foreach ( array_keys(get_object_vars($post)) as $field )
- $post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context);
- $post->filter = $context;
- } else {
- // Check if post already filtered for this context
- if ( isset($post['filter']) && $context == $post['filter'] )
- return $post;
- if ( !isset($post['ID']) )
- $post['ID'] = 0;
- foreach ( array_keys($post) as $field )
- $post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context);
- $post['filter'] = $context;
- }
- return $post;
- }
- /**
- * Sanitize post field based on context.
- *
- * Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and 'js'. The
- * 'display' context is used by default. 'attribute' and 'js' contexts are treated like 'display'
- * when calling filters.
- *
- * @since 2.3.0
- * @uses apply_filters() Calls 'edit_$field' and '${field_no_prefix}_edit_pre' passing $value and
- * $post_id if $context == 'edit' and field name prefix == 'post_'.
- *
- * @uses apply_filters() Calls 'edit_post_$field' passing $value and $post_id if $context == 'db'.
- * @uses apply_filters() Calls 'pre_$field' passing $value if $context == 'db' and field name prefix == 'post_'.
- * @uses apply_filters() Calls '${field}_pre' passing $value if $context == 'db' and field name prefix != 'post_'.
- *
- * @uses apply_filters() Calls '$field' passing $value, $post_id and $context if $context == anything
- * other than 'raw', 'edit' and 'db' and field name prefix == 'post_'.
- * @uses apply_filters() Calls 'post_$field' passing $value if $context == anything other than 'raw',
- * 'edit' and 'db' and field name prefix != 'post_'.
- *
- * @param string $field The Post Object field name.
- * @param mixed $value The Post Object value.
- * @param int $post_id Post ID.
- * @param string $context How to sanitize post fields. Looks for 'raw', 'edit', 'db', 'display',
- * 'attribute' and 'js'.
- * @return mixed Sanitized value.
- */
- function sanitize_post_field($field, $value, $post_id, $context) {
- $int_fields = array('ID', 'post_parent', 'menu_order');
- if ( in_array($field, $int_fields) )
- $value = (int) $value;
- // Fields which contain arrays of ints.
- $array_int_fields = array( 'ancestors' );
- if ( in_array($field, $array_int_fields) ) {
- $value = array_map( 'absint', $value);
- return $value;
- }
- if ( 'raw' == $context )
- return $value;
- $prefixed = false;
- if ( false !== strpos($field, 'post_') ) {
- $prefixed = true;
- $field_no_prefix = str_replace('post_', '', $field);
- }
- if ( 'edit' == $context ) {
- $format_to_edit = array('post_content', 'post_excerpt', 'post_title', 'post_password');
- if ( $prefixed ) {
- $value = apply_filters("edit_$field", $value, $post_id);
- // Old school
- $value = apply_filters("${field_no_prefix}_edit_pre", $value, $post_id);
- } else {
- $value = apply_filters("edit_post_$field", $value, $post_id);
- }
- if ( in_array($field, $format_to_edit) ) {
- if ( 'post_content' == $field )
- $value = format_to_edit($value, user_can_richedit());
- else
- $value = format_to_edit($value);
- } else {
- $value = esc_attr($value);
- }
- } else if ( 'db' == $context ) {
- if ( $prefixed ) {
- $value = apply_filters("pre_$field", $value);
- $value = apply_filters("${field_no_prefix}_save_pre", $value);
- } else {
- $value = apply_filters("pre_post_$field", $value);
- $value = apply_filters("${field}_pre", $value);
- }
- } else {
- // Use display filters by default.
- if ( $prefixed )
- $value = apply_filters($field, $value, $post_id, $context);
- else
- $value = apply_filters("post_$field", $value, $post_id, $context);
- }
- if ( 'attribute' == $context )
- $value = esc_attr($value);
- else if ( 'js' == $context )
- $value = esc_js($value);
- return $value;
- }
- /**
- * Make a post sticky.
- *
- * Sticky posts should be displayed at the top of the front page.
- *
- * @since 2.7.0
- *
- * @param int $post_id Post ID.
- */
- function stick_post($post_id) {
- $stickies = get_option('sticky_posts');
- if ( !is_array($stickies) )
- $stickies = array($post_id);
- if ( ! in_array($post_id, $stickies) )
- $stickies[] = $post_id;
- update_option('sticky_posts', $stickies);
- }
- /**
- * Unstick a post.
- *
- * Sticky posts should be displayed at the top of the front page.
- *
- * @since 2.7.0
- *
- * @param int $post_id Post ID.
- */
- function unstick_post($post_id) {
- $stickies = get_option('sticky_posts');
- if ( !is_array($stickies) )
- return;
- if ( ! in_array($post_id, $stickies) )
- return;
- $offset = array_search($post_id, $stickies);
- if ( false === $offset )
- return;
- array_splice($stickies, $offset, 1);
- update_option('sticky_posts', $stickies);
- }
- /**
- * Count number of posts of a post type and is user has permissions to view.
- *
- * This function provides an efficient method of finding the amount of post's
- * type a blog has. Another method is to count the amount of items in
- * get_posts(), but that method has a lot of overhead with doing so. Therefore,
- * when developing for 2.5+, use this function instead.
- *
- * The $perm parameter checks for 'readable' value and if the user can read
- * private posts, it will display that for the user that is signed in.
- *
- * @since 2.5.0
- * @link http://codex.wordpress.org/Template_Tags/wp_count_posts
- *
- * @param string $type Optional. Post type to retrieve count
- * @param string $perm Optional. 'readable' or empty.
- * @return object Number of posts for each status
- */
- function wp_count_posts( $type = 'post', $perm = '' ) {
- global $wpdb;
- $user = wp_get_current_user();
- $cache_key = $type;
- $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
- if ( 'readable' == $perm && is_user_logged_in() ) {
- $post_type_object = get_post_type_object($type);
- if ( !current_user_can("read_private_{$post_type_object->capability_type}s") ) {
- $cache_key .= '_' . $perm . '_' . $user->ID;
- $query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))";
- }
- }
- $query .= ' GROUP BY post_status';
- $count = wp_cache_get($cache_key, 'counts');
- if ( false !== $count )
- return $count;
- $count = $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
- $stats = array();
- foreach ( get_post_stati() as $state )
- $stats[$state] = 0;
- foreach ( (array) $count as…
Large files files are truncated, but you can click here to view the full file