3 * Post functions and post utility function.
5 * Warning: The inline documentation for the functions contained
6 * in this file might be inaccurate, so the documentation is not
7 * authoritative at the moment.
15 * Retrieve attached file path based on attachment ID.
17 * You can optionally send it through the 'get_attached_file' filter, but by
18 * default it will just return the file path unfiltered.
20 * The function works by getting the single post meta name, named
21 * '_wp_attached_file' and returning it. This is a convenience function to
22 * prevent looking up the meta name and provide a mechanism for sending the
23 * attached filename through a filter.
28 * @uses apply_filters() Calls 'get_attached_file' on file path and attachment ID
30 * @param int $attachment_id Attachment ID
31 * @param bool $unfiltered Whether to apply filters or not
32 * @return string The file path to the attached file.
34 function get_attached_file( $attachment_id, $unfiltered = false ) {
35 $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
38 return apply_filters( 'get_attached_file', $file, $attachment_id );
42 * Update attachment file path based on attachment ID.
44 * Used to update the file path of the attachment, which uses post meta name
45 * '_wp_attached_file' to store the path of the attachment.
50 * @uses apply_filters() Calls 'update_attached_file' on file path and attachment ID
52 * @param int $attachment_id Attachment ID
53 * @param string $file File path for the attachment
54 * @return bool False on failure, true on success.
56 function update_attached_file( $attachment_id, $file ) {
57 if ( !get_post( $attachment_id ) )
60 $file = apply_filters( 'update_attached_file', $file, $attachment_id );
62 return update_post_meta( $attachment_id, '_wp_attached_file', $file );
66 * Retrieve all children of the post parent ID.
68 * Normally, without any enhancements, the children would apply to pages. In the
69 * context of the inner workings of WordPress, pages, posts, and attachments
70 * share the same table, so therefore the functionality could apply to any one
71 * of them. It is then noted that while this function does not work on posts, it
72 * does not mean that it won't work on posts. It is recommended that you know
73 * what context you wish to retrieve the children of.
75 * Attachments may also be made the child of a post, so if that is an accurate
76 * statement (which needs to be verified), it would then be possible to get
77 * all of the attachments for a post. Attachments have since changed since
78 * version 2.5, so this is most likely unaccurate, but serves generally as an
79 * example of what is possible.
81 * The arguments listed as defaults are for this function and also of the
82 * get_posts() function. The arguments are combined with the get_children
83 * defaults and are then passed to the get_posts() function, which accepts
84 * additional arguments. You can replace the defaults in this function, listed
85 * below and the additional arguments listed in the get_posts() function.
87 * The 'post_parent' is the most important argument and important attention
88 * needs to be paid to the $args parameter. If you pass either an object or an
89 * integer (number), then just the 'post_parent' is grabbed and everything else
90 * is lost. If you don't specify any arguments, then it is assumed that you are
91 * in The Loop and the post parent will be grabbed for from the current post.
93 * The 'post_parent' argument is the ID to get the children. The 'numberposts'
94 * is the amount of posts to retrieve that has a default of '-1', which is
95 * used to get all of the posts. Giving a number higher than 0 will only
96 * retrieve that amount of posts.
98 * The 'post_type' and 'post_status' arguments can be used to choose what
99 * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress
100 * post types are 'post', 'pages', and 'attachments'. The 'post_status'
101 * argument will accept any post status within the write administration panels.
103 * @see get_posts() Has additional arguments that can be replaced.
104 * @internal Claims made in the long description might be inaccurate.
110 * @param mixed $args Optional. User defined arguments for replacing the defaults.
111 * @param string $output Optional. Constant for return type, either OBJECT (default), ARRAY_A, ARRAY_N.
112 * @return array|bool False on failure and the type will be determined by $output parameter.
114 function &get_children($args = '', $output = OBJECT) {
115 if ( empty( $args ) ) {
116 if ( isset( $GLOBALS['post'] ) ) {
117 $args = array('post_parent' => (int) $GLOBALS['post']->post_parent );
121 } elseif ( is_object( $args ) ) {
122 $args = array('post_parent' => (int) $args->post_parent );
123 } elseif ( is_numeric( $args ) ) {
124 $args = array('post_parent' => (int) $args);
128 'numberposts' => -1, 'post_type' => '',
129 'post_status' => '', 'post_parent' => 0
132 $r = wp_parse_args( $args, $defaults );
134 $children = get_posts( $r );
138 update_post_cache($children);
140 foreach ( $children as $key => $child )
141 $kids[$child->ID] =& $children[$key];
143 if ( $output == OBJECT ) {
145 } elseif ( $output == ARRAY_A ) {
146 foreach ( $kids as $kid )
147 $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
149 } elseif ( $output == ARRAY_N ) {
150 foreach ( $kids as $kid )
151 $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
159 * get_extended() - Get extended entry info (<!--more-->)
161 * {@internal Missing Long Description}}
167 * @param string $post {@internal Missing Description}}
168 * @return array {@internal Missing Description}}
170 function get_extended($post) {
171 //Match the new style more links
172 if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) {
173 list($main, $extended) = explode($matches[0], $post, 2);
179 // Strip leading and trailing whitespace
180 $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
181 $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
183 return array('main' => $main, 'extended' => $extended);
187 * get_post() - Retrieves post data given a post ID or post object.
189 * {@internal Missing Long Description}}
195 * @link http://codex.wordpress.org/Function_Reference/get_post
197 * @param int|object &$post post ID or post object
198 * @param string $output {@internal Missing Description}}
199 * @param string $filter {@internal Missing Description}}
200 * @return mixed {@internal Missing Description}}
202 function &get_post(&$post, $output = OBJECT, $filter = 'raw') {
206 if ( empty($post) ) {
207 if ( isset($GLOBALS['post']) )
208 $_post = & $GLOBALS['post'];
211 } elseif ( is_object($post) ) {
212 _get_post_ancestors($post);
213 wp_cache_add($post->ID, $post, 'posts');
217 if ( ! $_post = wp_cache_get($post, 'posts') ) {
218 $_post = & $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post));
221 _get_post_ancestors($_post);
222 wp_cache_add($_post->ID, $_post, 'posts');
226 $_post = sanitize_post($_post, $filter);
228 if ( $output == OBJECT ) {
230 } elseif ( $output == ARRAY_A ) {
231 $__post = get_object_vars($_post);
233 } elseif ( $output == ARRAY_N ) {
234 $__post = array_values(get_object_vars($_post));
242 * Retrieve ancestors of a post.
248 * @param int|object $post Post ID or post object
249 * @return array Ancestor IDs or empty array if none are found.
251 function get_post_ancestors($post) {
252 $post = get_post($post);
254 if ( !empty($post->ancestors) )
255 return $post->ancestors;
261 * Retrieve data from a post field based on Post ID.
263 * Examples of the post field will be, 'post_type', 'post_status', 'content',
264 * etc and based off of the post object property or key names.
266 * The context values are based off of the taxonomy filter functions and
267 * supported values are found within those functions.
272 * @uses sanitize_post_field() See for possible $context values.
274 * @param string $field Post field name
275 * @param id $post Post ID
276 * @param string $context Optional. How to filter the field. Default is display.
277 * @return WP_Error|string Value in post field or WP_Error on failure
279 function get_post_field( $field, $post, $context = 'display' ) {
281 $post = get_post( $post );
283 if ( is_wp_error($post) )
286 if ( !is_object($post) )
289 if ( !isset($post->$field) )
292 return sanitize_post_field($field, $post->$field, $post->ID, $context);
296 * Retrieve the mime type of an attachment based on the ID.
298 * This function can be used with any post type, but it makes more sense with
305 * @param int $ID Optional. Post ID.
306 * @return bool|string False on failure or returns the mime type
308 function get_post_mime_type($ID = '') {
309 $post = & get_post($ID);
311 if ( is_object($post) )
312 return $post->post_mime_type;
318 * Retrieve the post status based on the Post ID.
320 * If the post ID is of an attachment, then the parent post status will be given
327 * @param int $ID Post ID
328 * @return string|bool Post status or false on failure.
330 function get_post_status($ID = '') {
331 $post = get_post($ID);
333 if ( is_object($post) ) {
334 if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) )
335 return get_post_status($post->post_parent);
337 return $post->post_status;
344 * Retrieve all of the WordPress supported post statuses.
346 * Posts have a limited set of valid status values, this provides the
347 * post_status values and descriptions.
353 * @return array List of post statuses.
355 function get_post_statuses( ) {
357 'draft' => __('Draft'),
358 'pending' => __('Pending Review'),
359 'private' => __('Private'),
360 'publish' => __('Published')
367 * Retrieve all of the WordPress support page statuses.
369 * Pages have a limited set of valid status values, this provides the
370 * post_status values and descriptions.
376 * @return array List of page statuses.
378 function get_page_statuses( ) {
380 'draft' => __('Draft'),
381 'private' => __('Private'),
382 'publish' => __('Published')
389 * get_post_type() - Returns post type
391 * {@internal Missing Long Description}}
398 * @uses $posts {@internal Missing Description}}
400 * @param mixed $post post object or post ID
401 * @return mixed post type or false
403 function get_post_type($post = false) {
406 if ( false === $post )
408 elseif ( (int) $post )
409 $post = get_post($post, OBJECT);
411 if ( is_object($post) )
412 return $post->post_type;
418 * set_post_type() - Set post type
420 * {@internal Missing Long Description}}
427 * @uses $posts {@internal Missing Description}}
429 * @param mixed $post_id post ID
430 * @param mixed post type
431 * @return bool {@internal Missing Description}}
433 function set_post_type( $post_id = 0, $post_type = 'post' ) {
436 $post_type = sanitize_post_field('post_type', $post_type, $post_id, 'db');
437 $return = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_type = %s WHERE ID = %d", $post_type, $post_id) );
439 if ( 'page' == $post_type )
440 clean_page_cache($post_id);
442 clean_post_cache($post_id);
448 * get_posts() - Returns a number of posts
450 * {@internal Missing Long Description}}
456 * @link http://codex.wordpress.org/Template_Tags/get_posts
458 * @param array $args {@internal Missing Description}}
459 * @return array {@internal Missing Description}}
461 function get_posts($args = null) {
463 'numberposts' => 5, 'offset' => 0,
464 'category' => 0, 'orderby' => 'post_date',
465 'order' => 'DESC', 'include' => '',
466 'exclude' => '', 'meta_key' => '',
467 'meta_value' =>'', 'post_type' => 'post',
468 'post_parent' => 0, 'suppress_filters' => true
471 $r = wp_parse_args( $args, $defaults );
472 if ( empty( $r['post_status'] ) )
473 $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
474 if ( ! empty($r['numberposts']) )
475 $r['posts_per_page'] = $r['numberposts'];
476 if ( ! empty($r['category']) )
477 $r['cat'] = $r['category'];
478 if ( ! empty($r['include']) ) {
479 $incposts = preg_split('/[\s,]+/',$r['include']);
480 $r['posts_per_page'] = count($incposts); // only the number of posts included
481 $r['post__in'] = $incposts;
482 } elseif ( ! empty($r['exclude']) )
483 $r['post__not_in'] = preg_split('/[\s,]+/',$r['exclude']);
485 $get_posts = new WP_Query;
486 return $get_posts->query($r);
491 // Post meta functions
495 * add_post_meta() - adds metadata for post
497 * {@internal Missing Long Description}}
503 * @link http://codex.wordpress.org/Function_Reference/add_post_meta
505 * @param int $post_id post ID
506 * @param string $key {@internal Missing Description}}
507 * @param mixed $value {@internal Missing Description}}
508 * @param bool $unique whether to check for a value with the same key
509 * @return bool {@internal Missing Description}}
511 function add_post_meta($post_id, $meta_key, $meta_value, $unique = false) {
514 // make sure meta is added to the post, not a revision
515 if ( $the_post = wp_is_post_revision($post_id) )
516 $post_id = $the_post;
518 // expected_slashed ($meta_key)
519 $meta_key = stripslashes($meta_key);
521 if ( $unique && $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = %d", $meta_key, $post_id ) ) )
524 $meta_value = maybe_serialize($meta_value);
526 $wpdb->insert( $wpdb->postmeta, compact( 'post_id', 'meta_key', 'meta_value' ) );
528 wp_cache_delete($post_id, 'post_meta');
534 * delete_post_meta() - delete post metadata
536 * {@internal Missing Long Description}}
542 * @link http://codex.wordpress.org/Function_Reference/delete_post_meta
544 * @param int $post_id post ID
545 * @param string $key {@internal Missing Description}}
546 * @param mixed $value {@internal Missing Description}}
547 * @return bool {@internal Missing Description}}
549 function delete_post_meta($post_id, $key, $value = '') {
552 $post_id = absint( $post_id );
554 // expected_slashed ($key, $value)
555 $key = stripslashes( $key );
556 $value = stripslashes( $value );
558 if ( empty( $value ) )
559 $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $key ) );
561 $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s AND meta_value = %s", $post_id, $key, $value ) );
566 if ( empty( $value ) )
567 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $key ) );
569 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s AND meta_value = %s", $post_id, $key, $value ) );
571 wp_cache_delete($post_id, 'post_meta');
577 * get_post_meta() - Get a post meta field
579 * {@internal Missing Long Description}}
585 * @link http://codex.wordpress.org/Function_Reference/get_post_meta
587 * @param int $post_id post ID
588 * @param string $key The meta key to retrieve
589 * @param bool $single Whether to return a single value
590 * @return mixed {@internal Missing Description}}
592 function get_post_meta($post_id, $key, $single = false) {
593 $post_id = (int) $post_id;
595 $meta_cache = wp_cache_get($post_id, 'post_meta');
597 if ( !$meta_cache ) {
598 update_postmeta_cache($post_id);
599 $meta_cache = wp_cache_get($post_id, 'post_meta');
602 if ( isset($meta_cache[$key]) ) {
604 return maybe_unserialize( $meta_cache[$key][0] );
606 return array_map('maybe_unserialize', $meta_cache[$key]);
614 * update_post_meta() - Update a post meta field
616 * {@internal Missing Long Description}}
622 * @link http://codex.wordpress.org/Function_Reference/update_post_meta
624 * @param int $post_id post ID
625 * @param string $key {@internal Missing Description}}
626 * @param mixed $value {@internal Missing Description}}
627 * @param mixed $prev_value previous value (for differentiating between meta fields with the same key and post ID)
628 * @return bool {@internal Missing Description}}
630 function update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '') {
633 // expected_slashed ($meta_key)
634 $meta_key = stripslashes($meta_key);
636 if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = %d", $meta_key, $post_id ) ) ) {
637 return add_post_meta($post_id, $meta_key, $meta_value);
640 $meta_value = maybe_serialize($meta_value);
642 $data = compact( 'meta_value' );
643 $where = compact( 'meta_key', 'post_id' );
645 if ( !empty( $prev_value ) ) {
646 $prev_value = maybe_serialize($prev_value);
647 $where['meta_value'] = $prev_value;
650 $wpdb->update( $wpdb->postmeta, $data, $where );
651 wp_cache_delete($post_id, 'post_meta');
656 * delete_post_meta_by_key() - Delete everything from post meta matching $post_meta_key
663 * @param string $post_meta_key What to search for when deleting
664 * @return bool Whether the post meta key was deleted from the database
666 function delete_post_meta_by_key($post_meta_key) {
668 if ( $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key)) ) {
669 /** @todo Get post_ids and delete cache */
670 // wp_cache_delete($post_id, 'post_meta');
677 * get_post_custom() - Retrieve post custom fields
679 * {@internal Missing Long Description}}
684 * @link http://codex.wordpress.org/Function_Reference/get_post_custom
689 * @param int $post_id post ID
690 * @return array {@internal Missing Description}}
692 function get_post_custom($post_id = 0) {
696 $post_id = (int) $id;
698 $post_id = (int) $post_id;
700 if ( ! wp_cache_get($post_id, 'post_meta') )
701 update_postmeta_cache($post_id);
703 return wp_cache_get($post_id, 'post_meta');
707 * get_post_custom_keys() - Retrieve post custom field names
712 * @link http://codex.wordpress.org/Function_Reference/get_post_custom_keys
714 * @param int $post_id post ID
715 * @return array|null Either array of the keys, or null if keys would not be retrieved
717 function get_post_custom_keys( $post_id = 0 ) {
718 $custom = get_post_custom( $post_id );
720 if ( !is_array($custom) )
723 if ( $keys = array_keys($custom) )
728 * get_post_custom_values() - Retrieve values for a custom post field
733 * @link http://codex.wordpress.org/Function_Reference/get_post_custom_values
735 * @param string $key field name
736 * @param int $post_id post ID
737 * @return mixed {@internal Missing Description}}
739 function get_post_custom_values( $key = '', $post_id = 0 ) {
740 $custom = get_post_custom($post_id);
742 return $custom[$key];
746 * sanitize_post() - Sanitize every post field
748 * {@internal Missing Long Description}}
754 * @param object|array $post The Post Object or Array
755 * @param string $context How to sanitize post fields
756 * @return object|array The now sanitized Post Object or Array (will be the same type as $post)
758 function sanitize_post($post, $context = 'display') {
759 if ( 'raw' == $context )
761 if ( is_object($post) ) {
762 foreach ( array_keys(get_object_vars($post)) as $field )
763 $post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context);
765 foreach ( array_keys($post) as $field )
766 $post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context);
772 * sanitize_post_field() - Sanitize post field based on context
774 * {@internal Missing Long Description}}
780 * @param string $field The Post Object field name
781 * @param string $value The Post Object value
782 * @param int $postid Post ID
783 * @param string $context How to sanitize post fields
784 * @return string Sanitized value
786 function sanitize_post_field($field, $value, $post_id, $context) {
787 $int_fields = array('ID', 'post_parent', 'menu_order');
788 if ( in_array($field, $int_fields) )
789 $value = (int) $value;
791 if ( 'raw' == $context )
795 if ( false !== strpos($field, 'post_') ) {
797 $field_no_prefix = str_replace('post_', '', $field);
800 if ( 'edit' == $context ) {
801 $format_to_edit = array('post_content', 'post_excerpt', 'post_title', 'post_password');
804 $value = apply_filters("edit_$field", $value, $post_id);
806 $value = apply_filters("${field_no_prefix}_edit_pre", $value, $post_id);
808 $value = apply_filters("edit_post_$field", $value, $post_id);
811 if ( in_array($field, $format_to_edit) ) {
812 if ( 'post_content' == $field )
813 $value = format_to_edit($value, user_can_richedit());
815 $value = format_to_edit($value);
817 $value = attribute_escape($value);
819 } else if ( 'db' == $context ) {
821 $value = apply_filters("pre_$field", $value);
822 $value = apply_filters("${field_no_prefix}_save_pre", $value);
824 $value = apply_filters("pre_post_$field", $value);
825 $value = apply_filters("${field}_pre", $value);
828 // Use display filters by default.
830 $value = apply_filters($field, $value, $post_id, $context);
832 $value = apply_filters("post_$field", $value, $post_id, $context);
835 if ( 'attribute' == $context )
836 $value = attribute_escape($value);
837 else if ( 'js' == $context )
838 $value = js_escape($value);
844 * Count number of posts of a post type and is user has permissions to view.
846 * This function provides an efficient method of finding the amount of post's
847 * type a blog has. Another method is to count the amount of items in
848 * get_posts(), but that method has a lot of overhead with doing so. Therefore,
849 * when developing for 2.5+, use this function instead.
851 * The $perm parameter checks for 'readable' value and if the user can read
852 * private posts, it will display that for the user that is signed in.
857 * @link http://codex.wordpress.org/Template_Tags/wp_count_posts
859 * @param string $type Optional. Post type to retrieve count
860 * @param string $perm Optional. 'readable' or empty.
861 * @return object Number of posts for each status
863 function wp_count_posts( $type = 'post', $perm = '' ) {
866 $user = wp_get_current_user();
870 $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
871 if ( 'readable' == $perm && is_user_logged_in() ) {
872 if ( !current_user_can("read_private_{$type}s") ) {
873 $cache_key .= '_' . $perm . '_' . $user->ID;
874 $query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))";
877 $query .= ' GROUP BY post_status';
879 $count = wp_cache_get($cache_key, 'counts');
880 if ( false !== $count )
883 $count = $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
886 foreach( (array) $count as $row_num => $row ) {
887 $stats[$row['post_status']] = $row['num_posts'];
890 $stats = (object) $stats;
891 wp_cache_set($cache_key, $stats, 'counts');
898 * wp_count_attachments() - Count number of attachments
900 * {@internal Missing Long Description}}
906 * @param string|array $post_mime_type Array or comma-separated list of MIME patterns
907 * @return array Number of posts for each post_mime_type
910 function wp_count_attachments( $mime_type = '' ) {
913 $and = wp_post_mime_type_where( $mime_type );
914 $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' $and GROUP BY post_mime_type", ARRAY_A );
917 foreach( (array) $count as $row ) {
918 $stats[$row['post_mime_type']] = $row['num_posts'];
921 return (object) $stats;
925 * wp_match_mime_type() - Check a MIME-Type against a list
927 * {@internal Missing Long Description}}
933 * @param string|array $wildcard_mime_types e.g. audio/mpeg or image (same as image/*) or flash (same as *flash*)
934 * @param string|array $real_mime_types post_mime_type values
935 * @return array array(wildcard=>array(real types))
937 function wp_match_mime_types($wildcard_mime_types, $real_mime_types) {
939 if ( is_string($wildcard_mime_types) )
940 $wildcard_mime_types = array_map('trim', explode(',', $wildcard_mime_types));
941 if ( is_string($real_mime_types) )
942 $real_mime_types = array_map('trim', explode(',', $real_mime_types));
943 $wild = '[-._a-z0-9]*';
944 foreach ( (array) $wildcard_mime_types as $type ) {
945 $type = str_replace('*', $wild, $type);
946 $patternses[1][$type] = "^$type$";
947 if ( false === strpos($type, '/') ) {
948 $patternses[2][$type] = "^$type/";
949 $patternses[3][$type] = $type;
953 foreach ( $patternses as $patterns )
954 foreach ( $patterns as $type => $pattern )
955 foreach ( (array) $real_mime_types as $real )
956 if ( preg_match("#$pattern#", $real) && ( empty($matches[$type]) || false === array_search($real, $matches[$type]) ) )
957 $matches[$type][] = $real;
962 * wp_get_post_mime_type_where() - Convert MIME types into SQL
968 * @param string|array $mime_types MIME types
969 * @return string SQL AND clause
971 function wp_post_mime_type_where($post_mime_types) {
973 $wildcards = array('', '%', '%/%');
974 if ( is_string($post_mime_types) )
975 $post_mime_types = array_map('trim', explode(',', $post_mime_types));
976 foreach ( (array) $post_mime_types as $mime_type ) {
977 $mime_type = preg_replace('/\s/', '', $mime_type);
978 $slashpos = strpos($mime_type, '/');
979 if ( false !== $slashpos ) {
980 $mime_group = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, 0, $slashpos));
981 $mime_subgroup = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, $slashpos + 1));
982 if ( empty($mime_subgroup) )
983 $mime_subgroup = '*';
985 $mime_subgroup = str_replace('/', '', $mime_subgroup);
986 $mime_pattern = "$mime_group/$mime_subgroup";
988 $mime_pattern = preg_replace('/[^-*.a-zA-Z0-9]/', '', $mime_type);
989 if ( false === strpos($mime_pattern, '*') )
990 $mime_pattern .= '/*';
993 $mime_pattern = preg_replace('/\*+/', '%', $mime_pattern);
995 if ( in_array( $mime_type, $wildcards ) )
998 if ( false !== strpos($mime_pattern, '%') )
999 $wheres[] = "post_mime_type LIKE '$mime_pattern'";
1001 $wheres[] = "post_mime_type = '$mime_pattern'";
1003 if ( !empty($wheres) )
1004 $where = ' AND (' . join(' OR ', $wheres) . ') ';
1009 * wp_delete_post() - Deletes a Post
1011 * {@internal Missing Long Description}}
1013 * @package WordPress
1017 * @param int $postid post ID
1018 * @return mixed {@internal Missing Description}}
1020 function wp_delete_post($postid = 0) {
1021 global $wpdb, $wp_rewrite;
1023 if ( !$post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) )
1026 if ( 'attachment' == $post->post_type )
1027 return wp_delete_attachment($postid);
1029 do_action('delete_post', $postid);
1031 /** @todo delete for pluggable post taxonomies too */
1032 wp_delete_object_term_relationships($postid, array('category', 'post_tag'));
1034 $parent_data = array( 'post_parent' => $post->post_parent );
1035 $parent_where = array( 'post_parent' => $postid );
1037 if ( 'page' == $post->post_type) {
1038 // if the page is defined in option page_on_front or post_for_posts,
1039 // adjust the corresponding options
1040 if ( get_option('page_on_front') == $postid ) {
1041 update_option('show_on_front', 'posts');
1042 delete_option('page_on_front');
1044 if ( get_option('page_for_posts') == $postid ) {
1045 delete_option('page_for_posts');
1048 // Point children of this page to its parent, also clean the cache of affected children
1049 $children_query = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type='page'", $postid);
1050 $children = $wpdb->get_results($children_query);
1052 $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'page' ) );
1055 // Do raw query. wp_get_post_revisions() is filtered
1056 $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) );
1057 // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up.
1058 foreach ( $revision_ids as $revision_id )
1059 wp_delete_post_revision( $revision_id );
1061 // Point all attachments to this post up one level
1062 $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) );
1064 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $postid ));
1066 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ));
1068 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d", $postid ));
1070 if ( 'page' == $post->post_type ) {
1071 clean_page_cache($postid);
1073 foreach ( (array) $children as $child )
1074 clean_page_cache($child->ID);
1076 $wp_rewrite->flush_rules();
1078 clean_post_cache($postid);
1081 do_action('deleted_post', $postid);
1087 * wp_get_post_categories() - Retrieve the list of categories for a post
1089 * Compatibility layer for themes and plugins. Also an easy layer of abstraction
1090 * away from the complexity of the taxonomy layer.
1092 * @package WordPress
1096 * @uses wp_get_object_terms() Retrieves the categories. Args details can be found here
1098 * @param int $post_id Optional. The Post ID
1099 * @param array $args Optional. Overwrite the defaults
1100 * @return array {@internal Missing Description}}
1102 function wp_get_post_categories( $post_id = 0, $args = array() ) {
1103 $post_id = (int) $post_id;
1105 $defaults = array('fields' => 'ids');
1106 $args = wp_parse_args( $args, $defaults );
1108 $cats = wp_get_object_terms($post_id, 'category', $args);
1113 * wp_get_post_tags() - Retrieve the post tags
1115 * @package WordPress
1119 * @uses wp_get_object_terms() Gets the tags for returning. Args can be found here
1121 * @param int $post_id Optional. The Post ID
1122 * @param array $args Optional. Overwrite the defaults
1123 * @return mixed The tags the post has currently
1125 function wp_get_post_tags( $post_id = 0, $args = array() ) {
1126 $post_id = (int) $post_id;
1128 $defaults = array('fields' => 'all');
1129 $args = wp_parse_args( $args, $defaults );
1131 $tags = wp_get_object_terms($post_id, 'post_tag', $args);
1137 * wp_get_recent_posts() - Get the $num most recent posts
1139 * {@internal Missing Long Description}}
1141 * @package WordPress
1145 * @param int $num number of posts to get
1146 * @return array {@internal Missing Description}}
1148 function wp_get_recent_posts($num = 10) {
1151 // Set the limit clause, if we got a limit
1154 $limit = "LIMIT $num";
1157 $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' ORDER BY post_date DESC $limit";
1158 $result = $wpdb->get_results($sql,ARRAY_A);
1160 return $result?$result:array();
1164 * wp_get_single_post() - Get one post
1166 * {@internal Missing Long Description}}
1168 * @package WordPress
1173 * @param int $postid post ID
1174 * @param string $mode How to return result, either OBJECT, ARRAY_N, or ARRAY_A
1175 * @return object|array Post object or array holding post contents and information
1177 function wp_get_single_post($postid = 0, $mode = OBJECT) {
1178 $postid = (int) $postid;
1180 $post = get_post($postid, $mode);
1182 // Set categories and tags
1183 if($mode == OBJECT) {
1184 $post->post_category = wp_get_post_categories($postid);
1185 $post->tags_input = wp_get_post_tags($postid, array('fields' => 'names'));
1188 $post['post_category'] = wp_get_post_categories($postid);
1189 $post['tags_input'] = wp_get_post_tags($postid, array('fields' => 'names'));
1196 * wp_insert_post() - Insert a post
1198 * {@internal Missing Long Description}}
1200 * @package WordPress
1207 * @uses $allowedtags
1209 * @param array $postarr post contents
1210 * @return int post ID or 0 on error
1212 function wp_insert_post($postarr = array(), $wp_error = false) {
1213 global $wpdb, $wp_rewrite, $user_ID;
1215 $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID,
1216 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
1217 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '',
1218 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '');
1220 $postarr = wp_parse_args($postarr, $defaults);
1221 $postarr = sanitize_post($postarr, 'db');
1223 // export array as variables
1224 extract($postarr, EXTR_SKIP);
1226 // Are we updating or creating?
1228 if ( !empty($ID) ) {
1230 $previous_status = get_post_field('post_status', $ID);
1232 $previous_status = 'new';
1235 if ( ('' == $post_content) && ('' == $post_title) && ('' == $post_excerpt) ) {
1237 return new WP_Error('empty_content', __('Content, title, and excerpt are empty.'));
1242 // Make sure we set a valid category
1243 if (0 == count($post_category) || !is_array($post_category)) {
1244 $post_category = array(get_option('default_category'));
1247 if ( empty($post_author) )
1248 $post_author = $user_ID;
1250 if ( empty($post_status) )
1251 $post_status = 'draft';
1253 if ( empty($post_type) )
1254 $post_type = 'post';
1256 // Get the post ID and GUID
1258 $post_ID = (int) $ID;
1259 $guid = get_post_field( 'guid', $post_ID );
1262 // Create a valid post name. Drafts are allowed to have an empty
1264 if ( empty($post_name) ) {
1265 if ( 'draft' != $post_status )
1266 $post_name = sanitize_title($post_title);
1268 $post_name = sanitize_title($post_name);
1271 // If the post date is empty (due to having been new or a draft) and status is not 'draft', set date to now
1272 if ( empty($post_date) || '0000-00-00 00:00:00' == $post_date ) {
1273 if ( !in_array($post_status, array('draft', 'pending')) )
1274 $post_date = current_time('mysql');
1276 $post_date = '0000-00-00 00:00:00';
1279 if ( empty($post_date_gmt) || '0000-00-00 00:00:00' == $post_date_gmt ) {
1280 if ( !in_array($post_status, array('draft', 'pending')) )
1281 $post_date_gmt = get_gmt_from_date($post_date);
1283 $post_date_gmt = '0000-00-00 00:00:00';
1286 if ( $update || '0000-00-00 00:00:00' == $post_date ) {
1287 $post_modified = current_time( 'mysql' );
1288 $post_modified_gmt = current_time( 'mysql', 1 );
1290 $post_modified = $post_date;
1291 $post_modified_gmt = $post_date_gmt;
1294 if ( 'publish' == $post_status ) {
1295 $now = gmdate('Y-m-d H:i:59');
1296 if ( mysql2date('U', $post_date_gmt) > mysql2date('U', $now) )
1297 $post_status = 'future';
1300 if ( empty($comment_status) ) {
1302 $comment_status = 'closed';
1304 $comment_status = get_option('default_comment_status');
1306 if ( empty($ping_status) )
1307 $ping_status = get_option('default_ping_status');
1309 if ( isset($to_ping) )
1310 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
1314 if ( ! isset($pinged) )
1317 if ( isset($post_parent) )
1318 $post_parent = (int) $post_parent;
1322 if ( isset($menu_order) )
1323 $menu_order = (int) $menu_order;
1327 if ( !isset($post_password) )
1328 $post_password = '';
1330 if ( 'draft' != $post_status ) {
1331 $post_name_check = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1", $post_name, $post_type, $post_ID, $post_parent));
1333 if ($post_name_check || in_array($post_name, $wp_rewrite->feeds) ) {
1336 $alt_post_name = substr($post_name, 0, 200-(strlen($suffix)+1)). "-$suffix";
1337 $post_name_check = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1", $alt_post_name, $post_type, $post_ID, $post_parent));
1339 } while ($post_name_check);
1340 $post_name = $alt_post_name;
1344 // expected_slashed (everything!)
1345 $data = compact( array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'guid' ) );
1346 $data = stripslashes_deep( $data );
1347 $where = array( 'ID' => $post_ID );
1350 do_action( 'pre_post_update', $post_ID );
1351 if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) {
1353 return new WP_Error('db_update_error', __('Could not update post in the database'), $wpdb->last_error);
1358 $data['post_mime_type'] = stripslashes( $post_mime_type ); // This isn't in the update
1359 if ( false === $wpdb->insert( $wpdb->posts, $data ) ) {
1361 return new WP_Error('db_insert_error', __('Could not insert post into the database'), $wpdb->last_error);
1365 $post_ID = (int) $wpdb->insert_id;
1367 // use the newly generated $post_ID
1368 $where = array( 'ID' => $post_ID );
1371 if ( empty($post_name) && 'draft' != $post_status ) {
1372 $post_name = sanitize_title($post_title, $post_ID);
1373 $wpdb->update( $wpdb->posts, compact( 'post_name' ), $where );
1376 wp_set_post_categories( $post_ID, $post_category );
1377 wp_set_post_tags( $post_ID, $tags_input );
1379 $current_guid = get_post_field( 'guid', $post_ID );
1381 if ( 'page' == $post_type )
1382 clean_page_cache($post_ID);
1384 clean_post_cache($post_ID);
1387 if ( !$update && '' == $current_guid )
1388 $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
1390 $post = get_post($post_ID);
1392 if ( !empty($page_template) && 'page' == $post_type ) {
1393 $post->page_template = $page_template;
1394 $page_templates = get_page_templates();
1395 if ( 'default' != $page_template && !in_array($page_template, $page_templates) ) {
1397 return new WP_Error('invalid_page_template', __('The page template is invalid.'));
1401 update_post_meta($post_ID, '_wp_page_template', $page_template);
1404 wp_transition_post_status($post_status, $previous_status, $post);
1407 do_action('edit_post', $post_ID, $post);
1409 do_action('save_post', $post_ID, $post);
1410 do_action('wp_insert_post', $post_ID, $post);
1416 * wp_update_post() - Update a post
1418 * {@internal Missing Long Description}}
1420 * @package WordPress
1425 * @param array $postarr post data
1426 * @return int {@internal Missing Description}}
1428 function wp_update_post($postarr = array()) {
1429 if ( is_object($postarr) )
1430 $postarr = get_object_vars($postarr);
1432 // First, get all of the original fields
1433 $post = wp_get_single_post($postarr['ID'], ARRAY_A);
1435 // Escape data pulled from DB.
1436 $post = add_magic_quotes($post);
1438 // Passed post category list overwrites existing category list if not empty.
1439 if ( isset($postarr['post_category']) && is_array($postarr['post_category'])
1440 && 0 != count($postarr['post_category']) )
1441 $post_cats = $postarr['post_category'];
1443 $post_cats = $post['post_category'];
1445 // Drafts shouldn't be assigned a date unless explicitly done so by the user
1446 if ( in_array($post['post_status'], array('draft', 'pending')) && empty($postarr['edit_date']) && empty($postarr['post_date']) &&
1447 ('0000-00-00 00:00:00' == $post['post_date']) )
1450 $clear_date = false;
1452 // Merge old and new fields with new fields overwriting old ones.
1453 $postarr = array_merge($post, $postarr);
1454 $postarr['post_category'] = $post_cats;
1455 if ( $clear_date ) {
1456 $postarr['post_date'] = '';
1457 $postarr['post_date_gmt'] = '';
1460 if ($postarr['post_type'] == 'attachment')
1461 return wp_insert_attachment($postarr);
1463 return wp_insert_post($postarr);
1467 * wp_publish_post() - Mark a post as "published"
1469 * {@internal Missing Long Description}}
1471 * @package WordPress
1476 * @param int $post_id Post ID
1477 * @return int|null {@internal Missing Description}}
1479 function wp_publish_post($post_id) {
1482 $post = get_post($post_id);
1487 if ( 'publish' == $post->post_status )
1490 $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post_id ) );
1492 $old_status = $post->post_status;
1493 $post->post_status = 'publish';
1494 wp_transition_post_status('publish', $old_status, $post);
1496 // Update counts for the post's terms.
1497 foreach ( get_object_taxonomies('post') as $taxonomy ) {
1498 $terms = wp_get_object_terms($post_id, $taxonomy, 'fields=tt_ids');
1499 wp_update_term_count($terms, $taxonomy);
1502 do_action('edit_post', $post_id, $post);
1503 do_action('save_post', $post_id, $post);
1504 do_action('wp_insert_post', $post_id, $post);
1508 * check_and_publish_future_post() - check to make sure post has correct status before
1509 * passing it on to be published. Invoked by cron 'publish_future_post' event
1510 * This safeguard prevents cron from publishing drafts, etc.
1512 * {@internal Missing Long Description}}
1514 * @package WordPress
1519 * @param int $post_id Post ID
1520 * @return int|null {@internal Missing Description}}
1522 function check_and_publish_future_post($post_id) {
1524 $post = get_post($post_id);
1529 if ( 'future' != $post->post_status )
1532 return wp_publish_post($post_id);
1536 * wp_add_post_tags() - Adds the tags to a post
1538 * @uses wp_set_post_tags() Same first two paraeters, but the last parameter is always set to true.
1540 * @package WordPress
1544 * @param int $post_id Optional. Post ID
1545 * @param string $tags The tags to set for the post
1546 * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise
1548 function wp_add_post_tags($post_id = 0, $tags = '') {
1549 return wp_set_post_tags($post_id, $tags, true);
1553 * wp_set_post_tags() - Set the tags for a post
1555 * {@internal Missing Long Description}}
1557 * @package WordPress
1562 * @param int $post_id post ID
1563 * @param string $tags The tags to set for the post
1564 * @param bool $append If true, don't delete existing tags, just add on. If false, replace the tags with the new tags.
1565 * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise
1567 function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) {
1569 $post_id = (int) $post_id;
1576 $tags = (is_array($tags)) ? $tags : explode( ',', trim($tags, " \n\t\r\0\x0B,") );
1577 wp_set_object_terms($post_id, $tags, 'post_tag', $append);
1581 * wp_set_post_categories() - Set categories for a post
1583 * {@internal Missing Long Description}}
1585 * @package WordPress
1590 * @param int $post_ID post ID
1591 * @param array $post_categories
1592 * @return bool|mixed {@internal Missing Description}}
1594 function wp_set_post_categories($post_ID = 0, $post_categories = array()) {
1595 $post_ID = (int) $post_ID;
1596 // If $post_categories isn't already an array, make it one:
1597 if (!is_array($post_categories) || 0 == count($post_categories) || empty($post_categories))
1598 $post_categories = array(get_option('default_category'));
1599 else if ( 1 == count($post_categories) && '' == $post_categories[0] )
1602 $post_categories = array_map('intval', $post_categories);
1603 $post_categories = array_unique($post_categories);
1605 return wp_set_object_terms($post_ID, $post_categories, 'category');
1606 } // wp_set_post_categories()
1609 * wp_transition_post_status() - Change the post transition status
1611 * {@internal Missing Long Description}}
1613 * @package WordPress
1617 * @param string $new_status {@internal Missing Description}}
1618 * @param string $old_status {@internal Missing Description}}
1619 * @param int $post {@internal Missing Description}}
1621 function wp_transition_post_status($new_status, $old_status, $post) {
1622 if ( $new_status != $old_status ) {
1623 do_action('transition_post_status', $new_status, $old_status, $post);
1624 do_action("${old_status}_to_$new_status", $post);
1626 do_action("${new_status}_$post->post_type", $post->ID, $post);
1630 // Trackback and ping functions
1634 * add_ping() - Add a URL to those already pung
1636 * {@internal Missing Long Description}}
1638 * @package WordPress
1643 * @param int $post_id post ID
1644 * @param string $uri {@internal Missing Description}}
1645 * @return mixed {@internal Missing Description}}
1647 function add_ping($post_id, $uri) {
1649 $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id ));
1650 $pung = trim($pung);
1651 $pung = preg_split('/\s/', $pung);
1653 $new = implode("\n", $pung);
1654 $new = apply_filters('add_ping', $new);
1655 // expected_slashed ($new)
1656 $new = stripslashes($new);
1657 return $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post_id ) );
1661 * get_enclosed() - Get enclosures already enclosed for a post
1663 * {@internal Missing Long Description}}
1665 * @package WordPress
1670 * @param int $post_id post ID
1671 * @return array {@internal Missing Description}}
1673 function get_enclosed($post_id) {
1674 $custom_fields = get_post_custom( $post_id );
1676 if ( !is_array( $custom_fields ) )
1679 foreach ( $custom_fields as $key => $val ) {
1680 if ( 'enclosure' != $key || !is_array( $val ) )
1682 foreach( $val as $enc ) {
1683 $enclosure = split( "\n", $enc );
1684 $pung[] = trim( $enclosure[ 0 ] );
1687 $pung = apply_filters('get_enclosed', $pung);
1692 * get_pung() - Get URLs already pinged for a post
1694 * {@internal Missing Long Description}}
1696 * @package WordPress
1701 * @param int $post_id post ID
1702 * @return array {@internal Missing Description}}
1704 function get_pung($post_id) {
1706 $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id ));
1707 $pung = trim($pung);
1708 $pung = preg_split('/\s/', $pung);
1709 $pung = apply_filters('get_pung', $pung);
1714 * get_to_ping() - Get any URLs in the todo list
1716 * {@internal Missing Long Description}}
1718 * @package WordPress
1723 * @param int $post_id post ID
1724 * @return array {@internal Missing Description}}
1726 function get_to_ping($post_id) {
1728 $to_ping = $wpdb->get_var( $wpdb->prepare( "SELECT to_ping FROM $wpdb->posts WHERE ID = %d", $post_id ));
1729 $to_ping = trim($to_ping);
1730 $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY);
1731 $to_ping = apply_filters('get_to_ping', $to_ping);
1736 * trackback_url_list() - Do trackbacks for a list of urls
1738 * {@internal Missing Long Description}}
1740 * @package WordPress
1744 * @param string $tb_list comma separated list of URLs
1745 * @param int $post_id post ID
1747 function trackback_url_list($tb_list, $post_id) {
1748 if (!empty($tb_list)) {
1750 $postdata = wp_get_single_post($post_id, ARRAY_A);
1752 // import postdata as variables
1753 extract($postdata, EXTR_SKIP);
1756 $excerpt = strip_tags($post_excerpt?$post_excerpt:$post_content);
1758 if (strlen($excerpt) > 255) {
1759 $excerpt = substr($excerpt,0,252) . '...';
1762 $trackback_urls = explode(',', $tb_list);
1763 foreach($trackback_urls as $tb_url) {
1764 $tb_url = trim($tb_url);
1765 trackback($tb_url, stripslashes($post_title), $excerpt, $post_id);
1775 * get_all_page_ids() - Get a list of page IDs
1777 * {@internal Missing Long Description}}
1779 * @package WordPress
1784 * @return array {@internal Missing Description}}
1786 function get_all_page_ids() {
1789 if ( ! $page_ids = wp_cache_get('all_page_ids', 'posts') ) {
1790 $page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'page'");
1791 wp_cache_add('all_page_ids', $page_ids, 'posts');
1798 * get_page() - Retrieves page data given a page ID or page object
1800 * {@internal Missing Long Description}}
1802 * @package WordPress
1806 * @param mixed &$page page object or page ID
1807 * @param string $output what to output
1808 * @param string $filter How the return value should be filtered.
1809 * @return mixed {@internal Missing Description}}
1811 function &get_page(&$page, $output = OBJECT, $filter = 'raw') {
1812 if ( empty($page) ) {
1813 if ( isset( $GLOBALS['page'] ) && isset( $GLOBALS['page']->ID ) )
1814 return get_post($GLOBALS['page'], $output, $filter);
1819 return get_post($page, $output, $filter);
1823 * get_page_by_path() - Retrieves a page given its path
1825 * {@internal Missing Long Description}}
1827 * @package WordPress
1832 * @param string $page_path page path
1833 * @param string $output output type
1834 * @return mixed {@internal Missing Description}}
1836 function get_page_by_path($page_path, $output = OBJECT) {
1838 $page_path = rawurlencode(urldecode($page_path));
1839 $page_path = str_replace('%2F', '/', $page_path);
1840 $page_path = str_replace('%20', ' ', $page_path);
1841 $page_paths = '/' . trim($page_path, '/');
1842 $leaf_path = sanitize_title(basename($page_paths));
1843 $page_paths = explode('/', $page_paths);
1844 foreach($page_paths as $pathdir)
1845 $full_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);
1847 $pages = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = %s AND (post_type = 'page' OR post_type = 'attachment')", $leaf_path ));
1849 if ( empty($pages) )
1852 foreach ($pages as $page) {
1853 $path = '/' . $leaf_path;
1855 while ($curpage->post_parent != 0) {
1856 $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type='page'", $curpage->post_parent ));
1857 $path = '/' . $curpage->post_name . $path;
1860 if ( $path == $full_path )
1861 return get_page($page->ID, $output);
1868 * get_page_by_title() - Retrieve a page given its title
1870 * {@internal Missing Long Description}}
1872 * @package WordPress
1877 * @param string $page_title page title
1878 * @param string $output output type
1879 * @return mixed {@internal Missing Description}}
1881 function get_page_by_title($page_title, $output = OBJECT) {
1883 $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type='page'", $page_title ));
1885 return get_page($page, $output);
1891 * get_page_children() - Retrieve child pages
1893 * {@internal Missing Long Description}}
1895 * @package WordPress
1899 * @param int $page_id page ID
1900 * @param array $pages list of pages
1901 * @return array {@internal Missing Description}}
1903 function &get_page_children($page_id, $pages) {
1904 $page_list = array();
1905 foreach ( $pages as $page ) {
1906 if ( $page->post_parent == $page_id ) {
1907 $page_list[] = $page;
1908 if ( $children = get_page_children($page->ID, $pages) )
1909 $page_list = array_merge($page_list, $children);
1916 * get_page_hierarchy() - {@internal Missing Short Description}}
1918 * Fetches the pages returned as a FLAT list, but arranged in order of their hierarchy,
1919 * i.e., child parents immediately follow their parents.
1921 * @package WordPress
1925 * @param array $posts posts array
1926 * @param int $parent parent page ID
1927 * @return array {@internal Missing Description}}
1929 function get_page_hierarchy($posts, $parent = 0) {
1930 $result = array ( );
1931 if ($posts) { foreach ($posts as $post) {
1932 if ($post->post_parent == $parent) {
1933 $result[$post->ID] = $post->post_name;
1934 $children = get_page_hierarchy($posts, $post->ID);
1935 $result += $children; //append $children to $result
1942 * get_page_uri() - Builds a page URI
1944 * {@internal Missing Long Description}}
1946 * @package WordPress
1950 * @param int $page_id page ID
1951 * @return string {@internal Missing Description}}
1953 function get_page_uri($page_id) {
1954 $page = get_page($page_id);
1955 $uri = $page->post_name;
1957 // A page cannot be it's own parent.
1958 if ( $page->post_parent == $page->ID )
1961 while ($page->post_parent != 0) {
1962 $page = get_page($page->post_parent);
1963 $uri = $page->post_name . "/" . $uri;
1970 * get_pages() - Retrieve a list of pages
1972 * {@internal Missing Long Description}}
1974 * @package WordPress
1979 * @param mixed $args Optional. Array or string of options
1980 * @return array List of pages matching defaults or $args
1982 function &get_pages($args = '') {
1986 'child_of' => 0, 'sort_order' => 'ASC',
1987 'sort_column' => 'post_title', 'hierarchical' => 1,
1988 'exclude' => '', 'include' => '',
1989 'meta_key' => '', 'meta_value' => '',
1993 $r = wp_parse_args( $args, $defaults );
1994 extract( $r, EXTR_SKIP );
1996 $key = md5( serialize( $r ) );
1997 if ( $cache = wp_cache_get( 'get_pages', 'posts' ) )
1998 if ( isset( $cache[ $key ] ) )
1999 return apply_filters('get_pages', $cache[ $key ], $r );
2002 if ( !empty($include) ) {
2003 $child_of = 0; //ignore child_of, exclude, meta_key, and meta_value params if using include
2007 $hierarchical = false;
2008 $incpages = preg_split('/[\s,]+/',$include);
2009 if ( count($incpages) ) {
2010 foreach ( $incpages as $incpage ) {
2011 if (empty($inclusions))
2012 $inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpage);
2014 $inclusions .= $wpdb->prepare(' OR ID = %d ', $incpage);
2018 if (!empty($inclusions))
2022 if ( !empty($exclude) ) {
2023 $expages = preg_split('/[\s,]+/',$exclude);
2024 if ( count($expages) ) {
2025 foreach ( $expages as $expage ) {
2026 if (empty($exclusions))
2027 $exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expage);
2029 $exclusions .= $wpdb->prepare(' AND ID <> %d ', $expage);
2033 if (!empty($exclusions))
2037 if (!empty($authors)) {
2038 $post_authors = preg_split('/[\s,]+/',$authors);
2040 if ( count($post_authors) ) {
2041 foreach ( $post_authors as $post_author ) {
2042 //Do we have an author id or an author login?
2043 if ( 0 == intval($post_author) ) {
2044 $post_author = get_userdatabylogin($post_author);
2045 if ( empty($post_author) )
2047 if ( empty($post_author->ID) )
2049 $post_author = $post_author->ID;
2052 if ( '' == $author_query )
2053 $author_query = $wpdb->prepare(' post_author = %d ', $post_author);
2055 $author_query .= $wpdb->prepare(' OR post_author = %d ', $post_author);
2057 if ( '' != $author_query )
2058 $author_query = " AND ($author_query)";
2063 $where = "$exclusions $inclusions ";
2064 if ( ! empty( $meta_key ) || ! empty( $meta_value ) ) {
2065 $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )";
2067 // meta_key and met_value might be slashed
2068 $meta_key = stripslashes($meta_key);
2069 $meta_value = stripslashes($meta_value);
2070 if ( ! empty( $meta_key ) )
2071 $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $meta_key);
2072 if ( ! empty( $meta_value ) )
2073 $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_value = %s", $meta_value);
2076 $query = "SELECT * FROM $wpdb->posts $join WHERE (post_type = 'page' AND post_status = 'publish') $where ";
2077 $query .= $author_query;
2078 $query .= " ORDER BY " . $sort_column . " " . $sort_order ;
2080 $pages = $wpdb->get_results($query);
2082 if ( empty($pages) )
2083 return apply_filters('get_pages', array(), $r);
2086 update_page_cache($pages);
2088 if ( $child_of || $hierarchical )
2089 $pages = & get_page_children($child_of, $pages);
2091 $cache[ $key ] = $pages;
2092 wp_cache_set( 'get_pages', $cache, 'posts' );
2094 $pages = apply_filters('get_pages', $pages, $r);
2100 // Attachment functions
2104 * is_local_attachment() - Check if the attachment URI is local one and is really an attachment.
2106 * {@internal Missing Long Description}}
2108 * @package WordPress
2112 * @param string $url URL to check
2113 * @return bool {@internal Missing Description}}
2115 function is_local_attachment($url) {
2116 if (strpos($url, get_bloginfo('url')) === false)
2118 if (strpos($url, get_bloginfo('url') . '/?attachment_id=') !== false)
2120 if ( $id = url_to_postid($url) ) {
2121 $post = & get_post($id);
2122 if ( 'attachment' == $post->post_type )
2129 * wp_insert_attachment() - Insert an attachment
2131 * {@internal Missing Long Description}}
2133 * @package WordPress
2140 * @param object $object attachment object
2141 * @param string $file filename
2142 * @param int $post_parent parent post ID
2143 * @return int {@internal Missing Description}}
2145 function wp_insert_attachment($object, $file = false, $parent = 0) {
2146 global $wpdb, $user_ID;
2148 $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID,
2149 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
2150 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '',
2151 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '');
2153 $object = wp_parse_args($object, $defaults);
2154 if ( !empty($parent) )
2155 $object['post_parent'] = $parent;
2157 $object = sanitize_post($object, 'db');
2159 // export array as variables
2160 extract($object, EXTR_SKIP);
2162 // Make sure we set a valid category
2163 if (0 == count($post_category) || !is_array($post_category)) {
2164 $post_category = array(get_option('default_category'));
2167 if ( empty($post_author) )
2168 $post_author = $user_ID;
2170 $post_type = 'attachment';
2171 $post_status = 'inherit';
2173 // Are we updating or creating?
2175 if ( !empty($ID) ) {
2177 $post_ID = (int) $ID;
2180 // Create a valid post name.
2181 if ( empty($post_name) )
2182 $post_name = sanitize_title($post_title);
2184 $post_name = sanitize_title($post_name);
2186 // expected_slashed ($post_name)
2187 $post_name_check = $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'inherit' AND ID != %d LIMIT 1", $post_ID));
2189 if ($post_name_check) {
2191 while ($post_name_check) {
2192 $alt_post_name = $post_name . "-$suffix";
2193 // expected_slashed ($alt_post_name, $post_name)
2194 $post_name_check = $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'inherit' AND ID != %d AND post_parent = %d LIMIT 1", $post_ID, $post_parent));
2197 $post_name = $alt_post_name;
2200 if ( empty($post_date) )
2201 $post_date = current_time('mysql');
2202 if ( empty($post_date_gmt) )
2203 $post_date_gmt = current_time('mysql', 1);
2205 if ( empty($post_modified) )
2206 $post_modified = $post_date;
2207 if ( empty($post_modified_gmt) )
2208 $post_modified_gmt = $post_date_gmt;
2210 if ( empty($comment_status) ) {
2212 $comment_status = 'closed';
2214 $comment_status = get_option('default_comment_status');
2216 if ( empty($ping_status) )
2217 $ping_status = get_option('default_ping_status');
2219 if ( isset($to_ping) )
2220 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
2224 if ( isset($post_parent) )
2225 $post_parent = (int) $post_parent;
2229 if ( isset($menu_order) )
2230 $menu_order = (int) $menu_order;
2234 if ( !isset($post_password) )
2235 $post_password = '';
2237 if ( ! isset($pinged) )
2240 // expected_slashed (everything!)
2241 $data = compact( array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'guid' ) );
2242 $data = stripslashes_deep( $data );
2245 $wpdb->update( $wpdb->posts, $data, array( 'ID' => $post_ID ) );
2247 $wpdb->insert( $wpdb->posts, $data );
2248 $post_ID = (int) $wpdb->insert_id;
2251 if ( empty($post_name) ) {
2252 $post_name = sanitize_title($post_title, $post_ID);
2253 $wpdb->update( $wpdb->posts, compact("post_name"), array( 'ID' => $post_ID ) );
2256 wp_set_post_categories($post_ID, $post_category);
2259 update_attached_file( $post_ID, $file );
2261 clean_post_cache($post_ID);
2264 do_action('edit_attachment', $post_ID);
2266 do_action('add_attachment', $post_ID);
2273 * wp_delete_attachment() - Delete an attachment
2275 * {@internal Missing Long Description}}
2277 * @package WordPress
2282 * @param int $postid attachment Id
2283 * @return mixed {@internal Missing Description}}
2285 function wp_delete_attachment($postid) {
2288 if ( !$post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) )
2291 if ( 'attachment' != $post->post_type )
2294 $meta = wp_get_attachment_metadata( $postid );
2295 $file = get_attached_file( $postid );
2297 /** @todo Delete for pluggable post taxonomies too */
2298 wp_delete_object_term_relationships($postid, array('category', 'post_tag'));
2300 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $postid ));
2302 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ));
2304 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d ", $postid ));
2306 if ( ! empty($meta['thumb']) ) {
2307 // Don't delete the thumb if another attachment uses it
2308 if (! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%'.$meta['thumb'].'%', $postid)) ) {
2309 $thumbfile = str_replace(basename($file), $meta['thumb'], $file);
2310 $thumbfile = apply_filters('wp_delete_file', $thumbfile);
2311 @ unlink($thumbfile);
2315 // remove intermediate images if there are any
2316 $sizes = apply_filters('intermediate_image_sizes', array('thumbnail', 'medium'));
2317 foreach ( $sizes as $size ) {
2318 if ( $intermediate = image_get_intermediate_size($postid, $size) ) {
2319 $intermediate_file = apply_filters('wp_delete_file', $intermediate['path']);
2320 @ unlink($intermediate_file);
2324 $file = apply_filters('wp_delete_file', $file);
2326 if ( ! empty($file) )
2329 clean_post_cache($postid);
2331 do_action('delete_attachment', $postid);
2337 * wp_get_attachment_metadata() - Retrieve metadata for an attachment
2339 * {@internal Missing Long Description}}
2341 * @package WordPress
2345 * @param int $post_id attachment ID
2346 * @param bool $unfiltered Optional, default is false. If true, filters are not run
2347 * @return array {@internal Missing Description}}
2349 function wp_get_attachment_metadata( $post_id, $unfiltered = false ) {
2350 $post_id = (int) $post_id;
2351 if ( !$post =& get_post( $post_id ) )
2354 $data = get_post_meta( $post->ID, '_wp_attachment_metadata', true );
2357 return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID );
2361 * wp_update_attachment_metadata() - Update metadata for an attachment
2363 * {@internal Missing Long Description}}
2365 * @package WordPress
2369 * @param int $post_id attachment ID
2370 * @param array $data attachment data
2371 * @return int {@internal Missing Description}}
2373 function wp_update_attachment_metadata( $post_id, $data ) {
2374 $post_id = (int) $post_id;
2375 if ( !$post =& get_post( $post_id ) )
2378 $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID );
2380 return update_post_meta( $post->ID, '_wp_attachment_metadata', $data);
2384 * wp_get_attachment_url() - Retrieve the URL for an attachment
2386 * {@internal Missing Long Description}}
2388 * @package WordPress
2392 * @param int $post_id attachment ID
2393 * @return string {@internal Missing Description}}
2395 function wp_get_attachment_url( $post_id = 0 ) {
2396 $post_id = (int) $post_id;
2397 if ( !$post =& get_post( $post_id ) )
2400 $url = get_the_guid( $post->ID );
2402 if ( 'attachment' != $post->post_type || !$url )
2405 return apply_filters( 'wp_get_attachment_url', $url, $post->ID );
2409 * wp_get_attachment_thumb_file() - Retrieve thumbnail for an attachment
2411 * {@internal Missing Long Description}}
2413 * @package WordPress
2417 * @param int $post_id attachment ID
2418 * @return mixed {@internal Missing Description}}
2420 function wp_get_attachment_thumb_file( $post_id = 0 ) {
2421 $post_id = (int) $post_id;
2422 if ( !$post =& get_post( $post_id ) )
2424 if ( !is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) )
2427 $file = get_attached_file( $post->ID );
2429 if ( !empty($imagedata['thumb']) && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file)) && file_exists($thumbfile) )
2430 return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID );
2435 * wp_get_attachment_thumb_url() - Retrieve URL for an attachment thumbnail
2437 * {@internal Missing Long Description}}
2439 * @package WordPress
2443 * @param int $post_id attachment ID
2444 * @return string {@internal Missing Description}}
2446 function wp_get_attachment_thumb_url( $post_id = 0 ) {
2447 $post_id = (int) $post_id;
2448 if ( !$post =& get_post( $post_id ) )
2450 if ( !$url = wp_get_attachment_url( $post->ID ) )
2453 $sized = image_downsize( $post_id, 'thumbnail' );
2457 if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) )
2460 $url = str_replace(basename($url), basename($thumb), $url);
2462 return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID );
2466 * wp_attachment_is_image() - Check if the attachment is an image
2468 * {@internal Missing Long Description}}
2470 * @package WordPress
2474 * @param int $post_id attachment ID
2475 * @return bool {@internal Missing Description}}
2477 function wp_attachment_is_image( $post_id = 0 ) {
2478 $post_id = (int) $post_id;
2479 if ( !$post =& get_post( $post_id ) )
2482 if ( !$file = get_attached_file( $post->ID ) )
2485 $ext = preg_match('/\.([^.]+)$/', $file, $matches) ? strtolower($matches[1]) : false;
2487 $image_exts = array('jpg', 'jpeg', 'gif', 'png');
2489 if ( 'image/' == substr($post->post_mime_type, 0, 6) || $ext && 'import' == $post->post_mime_type && in_array($ext, $image_exts) )
2495 * wp_mime_type_icon() - Retrieve the icon for a MIME type
2497 * {@internal Missing Long Description}}
2499 * @package WordPress
2503 * @param string $mime MIME type
2504 * @return string|bool {@internal Missing Description}}
2506 function wp_mime_type_icon( $mime = 0 ) {
2507 if ( !is_numeric($mime) )
2508 $icon = wp_cache_get("mime_type_icon_$mime");
2509 if ( empty($icon) ) {
2511 $post_mimes = array();
2512 if ( is_numeric($mime) ) {
2513 $mime = (int) $mime;
2514 if ( $post =& get_post( $mime ) ) {
2515 $post_id = (int) $post->ID;
2516 $ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $post->guid);
2517 if ( !empty($ext) ) {
2518 $post_mimes[] = $ext;
2519 if ( $ext_type = wp_ext2type( $ext ) )
2520 $post_mimes[] = $ext_type;
2522 $mime = $post->post_mime_type;
2527 $post_mimes[] = $mime;
2530 $icon_files = wp_cache_get('icon_files');
2532 if ( !is_array($icon_files) ) {
2533 $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/crystal' );
2534 $icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url('images/crystal') );
2535 $dirs = apply_filters( 'icon_dirs', array($icon_dir => $icon_dir_uri) );
2536 $icon_files = array();
2538 $dir = array_shift($keys = array_keys($dirs));
2539 $uri = array_shift($dirs);
2540 if ( $dh = opendir($dir) ) {
2541 while ( false !== $file = readdir($dh) ) {
2542 $file = basename($file);
2543 if ( substr($file, 0, 1) == '.' )
2545 if ( !in_array(strtolower(substr($file, -4)), array('.png', '.gif', '.jpg') ) ) {
2546 if ( is_dir("$dir/$file") )
2547 $dirs["$dir/$file"] = "$uri/$file";
2550 $icon_files["$dir/$file"] = "$uri/$file";
2555 wp_cache_set('icon_files', $icon_files, 600);
2558 // Icon basename - extension = MIME wildcard
2559 foreach ( $icon_files as $file => $uri )
2560 $types[ preg_replace('/^([^.]*).*$/', '$1', basename($file)) ] =& $icon_files[$file];
2562 if ( ! empty($mime) ) {
2563 $post_mimes[] = substr($mime, 0, strpos($mime, '/'));
2564 $post_mimes[] = substr($mime, strpos($mime, '/') + 1);
2565 $post_mimes[] = str_replace('/', '_', $mime);
2568 $matches = wp_match_mime_types(array_keys($types), $post_mimes);
2569 $matches['default'] = array('default');
2571 foreach ( $matches as $match => $wilds ) {
2572 if ( isset($types[$wilds[0]])) {
2573 $icon = $types[$wilds[0]];
2574 if ( !is_numeric($mime) )
2575 wp_cache_set("mime_type_icon_$mime", $icon);
2581 return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id ); // Last arg is 0 if function pass mime type.
2585 * wp_check_for_changed_slugs() - {@internal Missing Short Description}}
2587 * {@internal Missing Long Description}}
2589 * @package WordPress
2593 * @param int $post_id The Post ID
2594 * @return int Same as $post_id
2596 function wp_check_for_changed_slugs($post_id) {
2597 if ( !isset($_POST['wp-old-slug']) || !strlen($_POST['wp-old-slug']) )
2600 $post = &get_post($post_id);
2602 // we're only concerned with published posts
2603 if ( $post->post_status != 'publish' || $post->post_type != 'post' )
2606 // only bother if the slug has changed
2607 if ( $post->post_name == $_POST['wp-old-slug'] )
2610 $old_slugs = (array) get_post_meta($post_id, '_wp_old_slug');
2612 // if we haven't added this old slug before, add it now
2613 if ( !count($old_slugs) || !in_array($_POST['wp-old-slug'], $old_slugs) )
2614 add_post_meta($post_id, '_wp_old_slug', $_POST['wp-old-slug']);
2616 // if the new slug was used previously, delete it from the list
2617 if ( in_array($post->post_name, $old_slugs) )
2618 delete_post_meta($post_id, '_wp_old_slug', $post->post_name);
2624 * get_private_posts_cap_sql() - {@internal Missing Short Description}}
2626 * This function provides a standardized way to appropriately select on
2627 * the post_status of posts/pages. The function will return a piece of
2628 * SQL code that can be added to a WHERE clause; this SQL is constructed
2629 * to allow all published posts, and all private posts to which the user
2632 * @package WordPress
2637 * @uses apply_filters() Call 'pub_priv_sql_capability' filter for plugins with different post types
2639 * @param string $post_type currently only supports 'post' or 'page'.
2640 * @return string SQL code that can be added to a where clause.
2642 function get_private_posts_cap_sql($post_type) {
2647 if ($post_type == 'post') {
2648 $cap = 'read_private_posts';
2650 } elseif ($post_type == 'page') {
2651 $cap = 'read_private_pages';
2652 // Dunno what it is, maybe plugins have their own post type?
2654 $cap = apply_filters('pub_priv_sql_capability', $cap);
2657 // We don't know what it is, filters don't change anything,
2658 // so set the SQL up to return nothing.
2663 $sql = '(post_status = \'publish\'';
2665 if (current_user_can($cap)) {
2666 // Does the user have the capability to view private posts? Guess so.
2667 $sql .= ' OR post_status = \'private\'';
2668 } elseif (is_user_logged_in()) {
2669 // Users can view their own private posts.
2670 $sql .= ' OR post_status = \'private\' AND post_author = \'' . $user_ID . '\'';
2679 * get_lastpostdate() - {@internal Missing Short Description}}
2681 * {@internal Missing Long Description}}
2683 * @package WordPress
2689 * @uses apply_filters() Calls 'get_lastpostdate' filter
2691 * @global mixed $cache_lastpostdate Stores the last post date
2692 * @global mixed $pagenow The current page being viewed
2694 * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
2695 * @return string The date of the last post.
2697 function get_lastpostdate($timezone = 'server') {
2698 global $cache_lastpostdate, $wpdb, $blog_id;
2699 $add_seconds_server = date('Z');
2700 if ( !isset($cache_lastpostdate[$blog_id][$timezone]) ) {
2701 switch(strtolower($timezone)) {
2703 $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
2706 $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
2709 $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
2712 $cache_lastpostdate[$blog_id][$timezone] = $lastpostdate;
2714 $lastpostdate = $cache_lastpostdate[$blog_id][$timezone];
2716 return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone );
2720 * get_lastpostmodified() - {@internal Missing Short Description}}
2722 * {@internal Missing Long Description}}
2724 * @package WordPress
2730 * @uses apply_filters() Calls 'get_lastpostmodified' filter
2732 * @global mixed $cache_lastpostmodified Stores the date the last post was modified
2733 * @global mixed $pagenow The current page being viewed
2735 * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
2736 * @return string The date the post was last modified.
2738 function get_lastpostmodified($timezone = 'server') {
2739 global $cache_lastpostmodified, $wpdb, $blog_id;
2740 $add_seconds_server = date('Z');
2741 if ( !isset($cache_lastpostmodified[$blog_id][$timezone]) ) {
2742 switch(strtolower($timezone)) {
2744 $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
2747 $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
2750 $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
2753 $lastpostdate = get_lastpostdate($timezone);
2754 if ( $lastpostdate > $lastpostmodified ) {
2755 $lastpostmodified = $lastpostdate;
2757 $cache_lastpostmodified[$blog_id][$timezone] = $lastpostmodified;
2759 $lastpostmodified = $cache_lastpostmodified[$blog_id][$timezone];
2761 return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone );
2765 * update_post_cache() - Updates posts in cache
2767 * @usedby update_page_cache() update_page_cache() aliased by this function.
2769 * @package WordPress
2773 * @param array $posts Array of post objects
2775 function update_post_cache(&$posts) {
2779 foreach ( $posts as $post )
2780 wp_cache_add($post->ID, $post, 'posts');
2784 * clean_post_cache() - Will clean the post in the cache
2786 * Cleaning means delete from the cache of the post. Will call to clean
2787 * the term object cache associated with the post ID.
2789 * @package WordPress
2793 * @uses do_action() Will call the 'clean_post_cache' hook action.
2795 * @param int $id The Post ID in the cache to clean
2797 function clean_post_cache($id) {
2801 wp_cache_delete($id, 'posts');
2802 wp_cache_delete($id, 'post_meta');
2804 clean_object_term_cache($id, 'post');
2806 wp_cache_delete( 'wp_get_archives', 'general' );
2808 do_action('clean_post_cache', $id);
2810 if ( $children = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d", $id) ) ) {
2811 foreach( $children as $cid )
2812 clean_post_cache( $cid );
2817 * update_page_cache() - Alias of update_post_cache()
2819 * @see update_post_cache() Posts and pages are the same, alias is intentional
2821 * @package WordPress
2825 * @param array $pages list of page objects
2827 function update_page_cache(&$pages) {
2828 update_post_cache($pages);
2832 * clean_page_cache() - Will clean the page in the cache
2834 * Clean (read: delete) page from cache that matches $id. Will also clean
2835 * cache associated with 'all_page_ids' and 'get_pages'.
2837 * @package WordPress
2841 * @uses do_action() Will call the 'clean_page_cache' hook action.
2843 * @param int $id Page ID to clean
2845 function clean_page_cache($id) {
2846 clean_post_cache($id);
2848 wp_cache_delete( 'all_page_ids', 'posts' );
2849 wp_cache_delete( 'get_pages', 'posts' );
2851 do_action('clean_page_cache', $id);
2855 * update_post_caches() - Call major cache updating functions for list of Post objects.
2857 * @package WordPress
2862 * @uses update_post_cache()
2863 * @uses update_object_term_cache()
2864 * @uses update_postmeta_cache()
2866 * @param array $posts Array of Post objects
2868 function update_post_caches(&$posts) {
2869 // No point in doing all this work if we didn't match any posts.
2873 update_post_cache($posts);
2875 $post_ids = array();
2877 for ($i = 0; $i < count($posts); $i++)
2878 $post_ids[] = $posts[$i]->ID;
2880 update_object_term_cache($post_ids, 'post');
2882 update_postmeta_cache($post_ids);
2886 * update_postmeta_cache() - {@internal Missing Short Description}}
2888 * {@internal Missing Long Description}}
2890 * @package WordPress
2896 * @param array $post_ids {@internal Missing Description}}
2897 * @return bool|array Returns false if there is nothing to update or an array of metadata
2899 function update_postmeta_cache($post_ids) {
2902 if ( empty( $post_ids ) )
2905 if ( !is_array($post_ids) ) {
2906 $post_ids = preg_replace('|[^0-9,]|', '', $post_ids);
2907 $post_ids = explode(',', $post_ids);
2910 $post_ids = array_map('intval', $post_ids);
2913 foreach ( (array) $post_ids as $id ) {
2914 if ( false === wp_cache_get($id, 'post_meta') )
2918 if ( empty( $ids ) )
2921 // Get post-meta info
2922 $id_list = join(',', $ids);
2924 if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id IN ($id_list) ORDER BY post_id, meta_key", ARRAY_A) ) {
2925 foreach ( (array) $meta_list as $metarow) {
2926 $mpid = (int) $metarow['post_id'];
2927 $mkey = $metarow['meta_key'];
2928 $mval = $metarow['meta_value'];
2930 // Force subkeys to be array type:
2931 if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) )
2932 $cache[$mpid] = array();
2933 if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) )
2934 $cache[$mpid][$mkey] = array();
2936 // Add a value to the current pid/key:
2937 $cache[$mpid][$mkey][] = $mval;
2941 foreach ( (array) $ids as $id ) {
2942 if ( ! isset($cache[$id]) )
2943 $cache[$id] = array();
2946 foreach ( array_keys($cache) as $post)
2947 wp_cache_set($post, $cache[$post], 'post_meta');
2957 * _transition_post_status() - Hook {@internal Missing Short Description}}
2959 * {@internal Missing Long Description}}
2961 * @package WordPress
2967 * @param string $new_status {@internal Missing Description}}
2968 * @param string $old_status {@internal Missing Description}}
2969 * @param object $post Object type containing the post information
2971 function _transition_post_status($new_status, $old_status, $post) {
2974 if ( $old_status != 'publish' && $new_status == 'publish' ) {
2975 // Reset GUID if transitioning to publish and it is empty
2976 if ( '' == get_the_guid($post->ID) )
2977 $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) );
2978 do_action('private_to_published', $post->ID); // Deprecated, use private_to_publish
2981 // Always clears the hook in case the post status bounced from future to draft.
2982 wp_clear_scheduled_hook('publish_future_post', $post->ID);
2986 * _future_post_hook() - Hook used to schedule publication for a post marked for the future.
2988 * The $post properties used and must exist are 'ID' and 'post_date_gmt'.
2990 * @package WordPress
2994 * @param int $post_id Not Used. Can be set to null.
2995 * @param object $post Object type containing the post information
2997 function _future_post_hook($deprecated = '', $post) {
2998 wp_clear_scheduled_hook( 'publish_future_post', $post->ID );
2999 wp_schedule_single_event(strtotime($post->post_date_gmt. ' GMT'), 'publish_future_post', array($post->ID));
3003 * _publish_post_hook() - Hook {@internal Missing Short Description}}
3005 * {@internal Missing Long Description}}
3007 * @package WordPress
3012 * @uses XMLRPC_REQUEST
3014 * @uses do_action Calls 'xmlprc_publish_post' action if XMLRPC_REQUEST is defined. Calls 'app_publish_post'
3015 * action if APP_REQUEST is defined.
3017 * @param int $post_id The ID in the database table of the post being published
3019 function _publish_post_hook($post_id) {
3022 if ( defined('XMLRPC_REQUEST') )
3023 do_action('xmlrpc_publish_post', $post_id);
3024 if ( defined('APP_REQUEST') )
3025 do_action('app_publish_post', $post_id);
3027 if ( defined('WP_IMPORTING') )
3030 $data = array( 'post_id' => $post_id, 'meta_value' => '1' );
3031 if ( get_option('default_pingback_flag') )
3032 $wpdb->insert( $wpdb->postmeta, $data + array( 'meta_key' => '_pingme' ) );
3033 $wpdb->insert( $wpdb->postmeta, $data + array( 'meta_key' => '_encloseme' ) );
3034 wp_schedule_single_event(time(), 'do_pings');
3038 * _save_post_hook() - Hook used to prevent page/post cache and rewrite rules from staying dirty
3040 * Does two things. If the post is a page and has a template then it will update/add that
3041 * template to the meta. For both pages and posts, it will clean the post cache to make sure
3042 * that the cache updates to the changes done recently. For pages, the rewrite rules of
3043 * WordPress are flushed to allow for any changes.
3045 * The $post parameter, only uses 'post_type' property and 'page_template' property.
3047 * @package WordPress
3051 * @uses $wp_rewrite Flushes Rewrite Rules.
3053 * @param int $post_id The ID in the database table for the $post
3054 * @param object $post Object type containing the post information
3056 function _save_post_hook($post_id, $post) {
3057 if ( $post->post_type == 'page' ) {
3058 clean_page_cache($post_id);
3060 $wp_rewrite->flush_rules();
3062 clean_post_cache($post_id);
3070 function _get_post_ancestors(&$_post) {
3073 if ( isset($_post->ancestors) )
3076 $_post->ancestors = array();
3078 if ( empty($_post->post_parent) || $_post->ID == $_post->post_parent )
3081 $id = $_post->ancestors[] = $_post->post_parent;
3082 while ( $ancestor = $wpdb->get_var( $wpdb->prepare("SELECT `post_parent` FROM $wpdb->posts WHERE ID = %d LIMIT 1", $id) ) ) {
3083 if ( $id == $ancestor )
3085 $id = $_post->ancestors[] = $ancestor;
3089 /* Post Revisions */
3092 * _wp_post_revision_fields() - determines which fields of posts are to be saved in revisions
3094 * Does two things. If passed a post *array*, it will return a post array ready to be
3095 * insterted into the posts table as a post revision.
3096 * Otherwise, returns an array whose keys are the post fields to be saved for post revisions.
3098 * @package WordPress
3099 * @subpackage Post Revisions
3102 * @param array $post optional a post array to be processed for insertion as a post revision
3103 * @param bool $autosave optional Is the revision an autosave?
3104 * @return array post array ready to be inserted as a post revision or array of fields that can be versioned
3106 function _wp_post_revision_fields( $post = null, $autosave = false ) {
3107 static $fields = false;
3110 // Allow these to be versioned
3112 'post_title' => __( 'Title' ),
3113 'post_content' => __( 'Content' ),
3114 'post_excerpt' => __( 'Excerpt' ),
3118 $fields = apply_filters( '_wp_post_revision_fields', $fields );
3120 // WP uses these internally either in versioning or elsewhere - they cannot be versioned
3121 foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect )
3122 unset( $fields[$protect] );
3125 if ( !is_array($post) )
3129 foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field )
3130 $return[$field] = $post[$field];
3132 $return['post_parent'] = $post['ID'];
3133 $return['post_status'] = 'inherit';
3134 $return['post_type'] = 'revision';
3135 $return['post_name'] = $autosave ? "$post[ID]-autosave" : "$post[ID]-revision";
3136 $return['post_date'] = $post['post_modified'];
3137 $return['post_date_gmt'] = $post['post_modified_gmt'];
3143 * wp_save_post_revision() - Saves an already existing post as a post revision. Typically used immediately prior to post updates.
3145 * @package WordPress
3146 * @subpackage Post Revisions
3149 * @uses _wp_put_post_revision()
3151 * @param int $post_id The ID of the post to save as a revision
3152 * @return mixed null or 0 if error, new revision ID if success
3154 function wp_save_post_revision( $post_id ) {
3155 // We do autosaves manually with wp_create_post_autosave()
3156 if ( @constant( 'DOING_AUTOSAVE' ) )
3159 // WP_POST_REVISIONS = 0, false
3160 if ( !constant('WP_POST_REVISIONS') )
3163 if ( !$post = get_post( $post_id, ARRAY_A ) )
3166 if ( !in_array( $post['post_type'], array( 'post', 'page' ) ) )
3169 $return = _wp_put_post_revision( $post );
3171 // WP_POST_REVISIONS = true (default), -1
3172 if ( !is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 )
3175 // all revisions and (possibly) one autosave
3176 $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
3178 // WP_POST_REVISIONS = (int) (# of autasaves to save)
3179 $delete = count($revisions) - WP_POST_REVISIONS;
3184 $revisions = array_slice( $revisions, 0, $delete );
3186 for ( $i = 0; isset($revisions[$i]); $i++ ) {
3187 if ( false !== strpos( $revisions[$i]->post_name, 'autosave' ) )
3189 wp_delete_post_revision( $revisions[$i]->ID );
3196 * wp_get_post_autosave() - returns the autosaved data of the specified post.
3198 * Returns a post object containing the information that was autosaved for the specified post.
3200 * @package WordPress
3201 * @subpackage Post Revisions
3204 * @param int $post_id The post ID
3205 * @return object|bool the autosaved data or false on failure or when no autosave exists
3207 function wp_get_post_autosave( $post_id ) {
3209 if ( !$post = get_post( $post_id ) )
3213 'name' => "{$post->ID}-autosave",
3214 'post_parent' => $post->ID,
3215 'post_type' => 'revision',
3216 'post_status' => 'inherit'
3219 // Use WP_Query so that the result gets cached
3220 $autosave_query = new WP_Query;
3222 add_action( 'parse_query', '_wp_get_post_autosave_hack' );
3223 $autosave = $autosave_query->query( $q );
3224 remove_action( 'parse_query', '_wp_get_post_autosave_hack' );
3226 if ( $autosave && is_array($autosave) && is_object($autosave[0]) )
3227 return $autosave[0];
3232 // Internally used to hack WP_Query into submission
3233 function _wp_get_post_autosave_hack( $query ) {
3234 $query->is_single = false;
3239 * wp_is_post_revision() - Determines if the specified post is a revision.
3241 * @package WordPress
3242 * @subpackage Post Revisions
3245 * @param int|object $post post ID or post object
3246 * @return bool|int false if not a revision, ID of revision's parent otherwise
3248 function wp_is_post_revision( $post ) {
3249 if ( !$post = wp_get_post_revision( $post ) )
3251 return (int) $post->post_parent;
3255 * wp_is_post_autosave() - Determines if the specified post is an autosave.
3257 * @package WordPress
3258 * @subpackage Post Revisions
3261 * @param int|object $post post ID or post object
3262 * @return bool|int false if not a revision, ID of autosave's parent otherwise
3264 function wp_is_post_autosave( $post ) {
3265 if ( !$post = wp_get_post_revision( $post ) )
3267 if ( "{$post->post_parent}-autosave" !== $post->post_name )
3269 return (int) $post->post_parent;
3273 * _wp_put_post_revision() - Inserts post data into the posts table as a post revision
3275 * @package WordPress
3276 * @subpackage Post Revisions
3279 * @uses wp_insert_post()
3281 * @param int|object|array $post post ID, post object OR post array
3282 * @param bool $autosave optional Is the revision an autosave?
3283 * @return mixed null or 0 if error, new revision ID if success
3285 function _wp_put_post_revision( $post = null, $autosave = false ) {
3286 if ( is_object($post) )
3287 $post = get_object_vars( $post );
3288 elseif ( !is_array($post) )
3289 $post = get_post($post, ARRAY_A);
3290 if ( !$post || empty($post['ID']) )
3293 if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
3294 return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
3296 $post = _wp_post_revision_fields( $post, $autosave );
3298 $revision_id = wp_insert_post( $post );
3299 if ( is_wp_error($revision_id) )
3300 return $revision_id;
3303 do_action( '_wp_put_post_revision', $revision_id );
3304 return $revision_id;
3308 * wp_get_post_revision() - Gets a post revision
3310 * @package WordPress
3311 * @subpackage Post Revisions
3316 * @param int|object $post post ID or post object
3317 * @param $output optional OBJECT, ARRAY_A, or ARRAY_N
3318 * @param string $filter optional sanitation filter. @see sanitize_post()
3319 * @return mixed null if error or post object if success
3321 function &wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
3323 if ( !$revision = get_post( $post, OBJECT, $filter ) )
3325 if ( 'revision' !== $revision->post_type )
3328 if ( $output == OBJECT ) {
3330 } elseif ( $output == ARRAY_A ) {
3331 $_revision = get_object_vars($revision);
3333 } elseif ( $output == ARRAY_N ) {
3334 $_revision = array_values(get_object_vars($revision));
3342 * wp_restore_post_revision() - Restores a post to the specified revision
3344 * Can restore a past using all fields of the post revision, or only selected fields.
3346 * @package WordPress
3347 * @subpackage Post Revisions
3350 * @uses wp_get_post_revision()
3351 * @uses wp_update_post()
3353 * @param int|object $revision_id revision ID or revision object
3354 * @param array $fields optional What fields to restore from. Defaults to all.
3355 * @return mixed null if error, false if no fields to restore, (int) post ID if success
3357 function wp_restore_post_revision( $revision_id, $fields = null ) {
3358 if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) )
3361 if ( !is_array( $fields ) )
3362 $fields = array_keys( _wp_post_revision_fields() );
3365 foreach( array_intersect( array_keys( $revision ), $fields ) as $field )
3366 $update[$field] = $revision[$field];
3371 $update['ID'] = $revision['post_parent'];
3373 $post_id = wp_update_post( $update );
3374 if ( is_wp_error( $post_id ) )
3378 do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );
3384 * wp_delete_post_revision() - Deletes a revision.
3386 * Deletes the row from the posts table corresponding to the specified revision
3388 * @package WordPress
3389 * @subpackage Post Revisions
3392 * @uses wp_get_post_revision()
3393 * @uses wp_delete_post()
3395 * @param int|object $revision_id revision ID or revision object
3396 * @param array $fields optional What fields to restore from. Defaults to all.
3397 * @return mixed null if error, false if no fields to restore, (int) post ID if success
3399 function wp_delete_post_revision( $revision_id ) {
3400 if ( !$revision = wp_get_post_revision( $revision_id ) )
3403 $delete = wp_delete_post( $revision->ID );
3404 if ( is_wp_error( $delete ) )
3408 do_action( 'wp_delete_post_revision', $revision->ID, $revision );
3414 * wp_get_post_revisions() - Returns all revisions of specified post
3416 * @package WordPress
3417 * @subpackage Post Revisions
3420 * @uses get_children()
3422 * @param int|object $post_id post ID or post object
3423 * @return array empty if no revisions
3425 function wp_get_post_revisions( $post_id = 0, $args = null ) {
3426 if ( !constant('WP_POST_REVISIONS') )
3428 if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) )
3431 $defaults = array( 'order' => 'DESC', 'orderby' => 'date' );
3432 $args = wp_parse_args( $args, $defaults );
3433 $args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) );
3435 if ( !$revisions = get_children( $args ) )