WordPressを読む 38-7 /blog/wp-includes/post.php 7
2014/12/25
目次
/blog/wp-includes/post.php 7
関数 wp_untrash_post()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /** * Restore a post or page from the Trash. * * @since 2.9.0 * * @param int $post_id Optional. Post ID. Default is ID of the global $post. * @return WP_Post|bool WP_Post object. False on failure. */ function wp_untrash_post( $post_id = 0 ) { if ( !$post = get_post($post_id, ARRAY_A) ) return $post; if ( $post['post_status'] != 'trash' ) return false; /** * Fires before a post is restored from the trash. * * @since 2.9.0 * * @param int $post_id Post ID. */ do_action( 'untrash_post', $post_id ); $post_status = get_post_meta($post_id, '_wp_trash_meta_status', true); $post['post_status'] = $post_status; delete_post_meta($post_id, '_wp_trash_meta_status'); delete_post_meta($post_id, '_wp_trash_meta_time'); wp_insert_post($post); wp_untrash_post_comments($post_id); /** * Fires after a post is restored from the trash. * * @since 2.9.0 * * @param int $post_id Post ID. */ do_action( 'untrashed_post', $post_id ); return $post; } |
関数 wp_trash_post_comments()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | /** * Moves comments for a post to the trash. * * @since 2.9.0 * * @global wpdb $wpdb WordPress database access abstraction object. * * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post. * @return mixed False on failure. */ function wp_trash_post_comments( $post = null ) { global $wpdb; $post = get_post($post); if ( empty($post) ) return; $post_id = $post->ID; /** * Fires before comments are sent to the trash. * * @since 2.9.0 * * @param int $post_id Post ID. */ do_action( 'trash_post_comments', $post_id ); $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id) ); if ( empty($comments) ) return; // Cache current status for each comment. $statuses = array(); foreach ( $comments as $comment ) $statuses[$comment->comment_ID] = $comment->comment_approved; add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses); // Set status for all comments to post-trashed. $result = $wpdb->update($wpdb->comments, array('comment_approved' => 'post-trashed'), array('comment_post_ID' => $post_id)); clean_comment_cache( array_keys($statuses) ); /** * Fires after comments are sent to the trash. * * @since 2.9.0 * * @param int $post_id Post ID. * @param array $statuses Array of comment statuses. */ do_action( 'trashed_post_comments', $post_id, $statuses ); return $result; } |
関数 wp_untrash_post_comments()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | /** * Restore comments for a post from the trash. * * @since 2.9.0 * * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post. * @return mixed False on failure. */ function wp_untrash_post_comments( $post = null ) { global $wpdb; $post = get_post($post); if ( empty($post) ) return; $post_id = $post->ID; $statuses = get_post_meta($post_id, '_wp_trash_meta_comments_status', true); if ( empty($statuses) ) return true; /** * Fires before comments are restored for a post from the trash. * * @since 2.9.0 * * @param int $post_id Post ID. */ do_action( 'untrash_post_comments', $post_id ); // Restore each comment to its original status. $group_by_status = array(); foreach ( $statuses as $comment_id => $comment_status ) $group_by_status[$comment_status][] = $comment_id; foreach ( $group_by_status as $status => $comments ) { // Sanity check. This shouldn't happen. if ( 'post-trashed' == $status ) $status = '0'; $comments_in = implode( "', '", $comments ); $wpdb->query( "UPDATE $wpdb->comments SET comment_approved = '$status' WHERE comment_ID IN ('" . $comments_in . "')" ); } clean_comment_cache( array_keys($statuses) ); delete_post_meta($post_id, '_wp_trash_meta_comments_status'); /** * Fires after comments are restored for a post from the trash. * * @since 2.9.0 * * @param int $post_id Post ID. */ do_action( 'untrashed_post_comments', $post_id ); } |
関数 wp_get_post_categories()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * Retrieve the list of categories for a post. * * Compatibility layer for themes and plugins. Also an easy layer of abstraction * away from the complexity of the taxonomy layer. * * @since 2.1.0 * * @see wp_get_object_terms() * * @param int $post_id Optional. The Post ID. Does not default to the ID of the * global $post. Default 0. * @param array $args Optional. Category arguments. Default empty. * @return array List of categories. */ function wp_get_post_categories( $post_id = 0, $args = array() ) { $post_id = (int) $post_id; $defaults = array('fields' => 'ids'); $args = wp_parse_args( $args, $defaults ); $cats = wp_get_object_terms($post_id, 'category', $args); return $cats; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * Retrieve the tags for a post. * * There is only one default for this function, called 'fields' and by default * is set to 'all'. There are other defaults that can be overridden in * {@link wp_get_object_terms()}. * * @since 2.3.0 * * @uses wp_get_object_terms() * * @param int $post_id Optional. The Post ID. Does not default to the ID of the * global $post. Defualt 0. * @param array $args Optional. Overwrite the defaults * @return array List of post tags. */ function wp_get_post_tags( $post_id = 0, $args = array() ) { return wp_get_post_terms( $post_id, 'post_tag', $args); } |
関数 wp_get_post_terms()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | /** * Retrieve the terms for a post. * * There is only one default for this function, called 'fields' and by default * is set to 'all'. There are other defaults that can be overridden in * {@link wp_get_object_terms()}. * * @since 2.8.0 * * @uses wp_get_object_terms() * * @param int $post_id Optional. The Post ID. Does not default to the ID of the * global $post. Default 0. * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'. * @param array $args Optional. {@link wp_get_object_terms()} arguments. Default empty array. * @return array List of post tags. */ function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) { $post_id = (int) $post_id; $defaults = array('fields' => 'all'); $args = wp_parse_args( $args, $defaults ); $tags = wp_get_object_terms($post_id, $taxonomy, $args); return $tags; } |
関数 wp_get_recent_posts()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | /** * Retrieve a number of recent posts. * * @since 1.0.0 * * @see get_posts() * * @param string $deprecated Not used. * @param array $args Optional. Arguments to retrieve posts. Default empty array. * @param string $output Optional. Type of output. Accepts ARRAY_A or ''. Default ARRAY_A. * @return array|bool Associative array if $output equals ARRAY_A, array or false if no results. */ function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) { if ( is_numeric( $args ) ) { _deprecated_argument( __FUNCTION__, '3.1', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) ); $args = array( 'numberposts' => absint( $args ) ); } // Set default arguments. $defaults = array( 'numberposts' => 10, 'offset' => 0, 'category' => 0, 'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '', 'meta_key' => '', 'meta_value' =>'', 'post_type' => 'post', 'post_status' => 'draft, publish, future, pending, private', 'suppress_filters' => true ); $r = wp_parse_args( $args, $defaults ); $results = get_posts( $r ); // Backward compatibility. Prior to 3.1 expected posts to be returned in array. if ( ARRAY_A == $output ){ foreach( $results as $key => $result ) { $results[$key] = get_object_vars( $result ); } return $results ? $results : array(); } return $results ? $results : false; } |