From 0982c3c0f9d66e50a7836dac3a71a912fe9fb238 Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 12 Oct 2010 18:33:14 +0000 Subject: [PATCH] Post formats, take one. see #14746 git-svn-id: http://svn.automattic.com/wordpress/trunk@15777 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/post.php | 39 +++++++++++++++++++++++++++++++++++++++ wp-includes/taxonomy.php | 13 +++++++++++++ 2 files changed, 52 insertions(+) diff --git a/wp-includes/post.php b/wp-includes/post.php index f6032a7c29..18e21302e0 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -470,6 +470,45 @@ function get_post_mime_type($ID = '') { return false; } +/** + * Retrieve the format for a post + * + * @param int|object $post A post + * + * @return mixed The format if successful. False if no format is set. WP_Error if errors. + */ +function get_post_format( $post ) { + $post = get_post($post); + + $format = wp_get_object_terms( $post->ID, 'post_format', array('orderby' => 'none', 'fields' => 'names') ); + + if ( is_wp_error($format) ) + return $format; + + if ( empty($format) ) + return false; + + return ( str_replace('post-format-', '', $format[0]) ); +} + +/** + * Assign a format to a post + * + * @param int|object $post The post for which to assign a format + * @param string $format A format to assign. + * @return mixed WP_Error on error. Array of affected term IDs on success. + */ +function set_post_format( $post, $format ) { + $post = get_post($post); + + if ( empty($post) ) + return new WP_Error('invalid_post', __('Invalid post')); + + $format = sanitize_key($format); + + return wp_set_post_terms($post->ID, array('post-format-' . $format), 'post_format'); +} + /** * Retrieve the post status based on the Post ID. * diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index a270b2407b..43d1a158aa 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -71,6 +71,19 @@ function create_initial_taxonomies() { 'show_ui' => false, '_builtin' => true, ) ) ; + + register_taxonomy( 'post_format', array('post', 'page', 'attachment'), array( + 'hierarchical' => false, + 'labels' => array( + 'name' => '', + 'singular_name' => '', + ), + 'query_var' => false, + 'rewrite' => false, + 'show_ui' => false, + '_builtin' => true, + 'show_in_nav_menus' => false, + ) ) ; } add_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority