479
* Retrieve the format slug for a post
483
* @param int|object $post A post
485
* @return mixed The format if successful. False if no format is set. WP_Error if errors.
487
function get_post_format( $post = null ) {
488
$post = get_post($post);
490
if ( ! post_type_supports( $post->post_type, 'post-formats' ) )
493
$_format = get_the_terms( $post->ID, 'post_format' );
495
if ( empty( $_format ) )
498
$format = array_shift( $_format );
500
return ( str_replace('post-format-', '', $format->slug ) );
504
* Check if a post has a particular format
509
* @param string $format The format to check for
510
* @param object|id $post The post to check. If not supplied, defaults to the current post if used in the loop.
511
* @return bool True if the post has the format, false otherwise.
513
function has_post_format( $format, $post = null ) {
514
return has_term('post-format-' . sanitize_key($format), 'post_format', $post);
518
* Assign a format to a post
522
* @param int|object $post The post for which to assign a format
523
* @param string $format A format to assign. Use an empty string or array to remove all formats from the post.
524
* @return mixed WP_Error on error. Array of affected term IDs on success.
526
function set_post_format( $post, $format ) {
527
$post = get_post($post);
530
return new WP_Error('invalid_post', __('Invalid post'));
532
if ( !empty($format) ) {
533
$format = sanitize_key($format);
534
if ( 'standard' == $format || !in_array( $format, array_keys( get_post_format_slugs() ) ) )
537
$format = 'post-format-' . $format;
540
return wp_set_post_terms($post->ID, $format, 'post_format');
474
544
* Retrieve the post status based on the Post ID.
476
546
* If the post ID is of an attachment, then the parent post status will be given
766
841
* Register a post type. Do not use before init.
768
* A simple function for creating or modifying a post type based on the
843
* A function for creating or modifying a post type based on the
769
844
* parameters given. The function will accept an array (second optional
770
845
* parameter), along with a string for the post type name.
773
847
* Optional $args contents:
775
849
* - label - Name of the post type shown in the menu. Usually plural. If not set, labels['name'] will be used.
776
850
* - description - A short descriptive summary of what the post type is. Defaults to blank.
777
851
* - public - Whether posts of this type should be shown in the admin UI. Defaults to false.
778
* - exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true if the type is not public, false if the type is public.
779
* - publicly_queryable - Whether post_type queries can be performed from the front page. Defaults to whatever public is set as.
780
* - show_ui - Whether to generate a default UI for managing this post type. Defaults to true if the type is public, false if the type is not public.
852
* - exclude_from_search - Whether to exclude posts with this post type from search results.
853
* Defaults to true if the type is not public, false if the type is public.
854
* - publicly_queryable - Whether post_type queries can be performed from the front page.
855
* Defaults to whatever public is set as.
856
* - show_ui - Whether to generate a default UI for managing this post type. Defaults to true
857
* if the type is public, false if the type is not public.
858
* - show_in_menu - Where to show the post type in the admin menu. True for a top level menu,
859
* false for no menu, or can be a top level page like 'tools.php' or 'edit.php?post_type=page'.
860
* show_ui must be true.
781
861
* - menu_position - The position in the menu order the post type should appear. Defaults to the bottom.
782
862
* - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
783
* - capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
784
* - capabilities - Array of capabilities for this post type. You can see accepted values in {@link get_post_type_capabilities()}. By default the capability_type is used to construct capabilities.
863
* - capability_type - The string to use to build the read, edit, and delete capabilities. Defaults to 'post'.
864
* May be passed as an array to allow for alternative plurals when using this argument as a base to construct the
865
* capabilities, e.g. array('story', 'stories').
866
* - capabilities - Array of capabilities for this post type. By default the capability_type is used
867
* as a base to construct capabilities. You can see accepted values in {@link get_post_type_capabilities()}.
868
* - map_meta_cap - Whether to use the internal default meta capability handling. Defaults to false.
785
869
* - hierarchical - Whether the post type is hierarchical. Defaults to false.
786
* - supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none.
787
* - register_meta_box_cb - Provide a callback function that will be called when setting up the meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback.
788
* - taxonomies - An array of taxonomy identifiers that will be registered for the post type. Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type().
789
* - labels - An array of labels for this post type. You can see accepted values in {@link get_post_type_labels()}. By default post labels are used for non-hierarchical types and page labels for hierarchical ones.
870
* - supports - An alias for calling add_post_type_support() directly. See {@link add_post_type_support()}
871
* for documentation. Defaults to none.
872
* - register_meta_box_cb - Provide a callback function that will be called when setting up the
873
* meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback.
874
* - taxonomies - An array of taxonomy identifiers that will be registered for the post type.
875
* Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or
876
* register_taxonomy_for_object_type().
877
* - labels - An array of labels for this post type. By default post labels are used for non-hierarchical
878
* types and page labels for hierarchical ones. You can see accepted values in {@link get_post_type_labels()}.
790
879
* - permalink_epmask - The default rewrite endpoint bitmasks.
791
* - rewrite - false to prevent rewrite, or array('slug'=>$slug) to customize permastruct; default will use $post_type as slug.
880
* - has_archive - True to enable post type archives. Will generate the proper rewrite rules if rewrite is enabled.
881
* - rewrite - false to prevent rewrite. Defaults to true. Use array('slug'=>$slug) to customize permastruct;
882
* default will use $post_type as slug. Other options include 'with_front', 'feeds', and 'pages'.
792
883
* - query_var - false to prevent queries, or string to value of the query var to use for this post type
793
884
* - can_export - true allows this post type to be exported.
794
885
* - show_in_nav_menus - true makes this post type available for selection in navigation menus.
795
* - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY!
796
* - _edit_link - URL segement to use for edit link of this post type. Set to 'post.php?post=%d'. THIS IS FOR INTERNAL USE ONLY!
886
* - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY!
887
* - _edit_link - URL segement to use for edit link of this post type. THIS IS FOR INTERNAL USE ONLY!
799
890
* @uses $wp_post_types Inserts new post type object into the list
801
892
* @param string $post_type Name of the post type.
802
893
* @param array|string $args See above description.
803
* @return object the registered post type object
894
* @return object|WP_Error the registered post type object, or an error object
805
896
function register_post_type($post_type, $args = array()) {
806
897
global $wp_post_types, $wp_rewrite, $wp;
811
902
// Args prefixed with an underscore are reserved for internal use.
812
903
$defaults = array(
813
904
'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null,
814
'_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'capabilities' => array(), 'hierarchical' => false,
815
'public' => false, 'rewrite' => true, 'query_var' => true, 'supports' => array(), 'register_meta_box_cb' => null,
905
'capability_type' => 'post', 'capabilities' => array(), 'map_meta_cap' => null,
906
'_builtin' => false, '_edit_link' => 'post.php?post=%d', 'hierarchical' => false,
907
'public' => false, 'rewrite' => true, 'has_archive' => false, 'query_var' => true,
908
'supports' => array(), 'register_meta_box_cb' => null,
816
909
'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null,
817
'permalink_epmask' => EP_PERMALINK, 'can_export' => true, 'show_in_nav_menus' => null
910
'permalink_epmask' => EP_PERMALINK, 'can_export' => true, 'show_in_nav_menus' => null, 'show_in_menu' => null,
819
912
$args = wp_parse_args($args, $defaults);
820
913
$args = (object) $args;
822
$post_type = sanitize_user($post_type, true);
915
$post_type = sanitize_key($post_type);
823
916
$args->name = $post_type;
918
if ( strlen( $post_type ) > 20 )
919
return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) );
825
921
// If not set, default to the setting for public.
826
922
if ( null === $args->publicly_queryable )
827
923
$args->publicly_queryable = $args->public;
862
969
if ( false !== $args->rewrite && '' != get_option('permalink_structure') ) {
863
if ( !is_array($args->rewrite) )
970
if ( ! is_array( $args->rewrite ) )
864
971
$args->rewrite = array();
865
if ( !isset($args->rewrite['slug']) )
972
if ( empty( $args->rewrite['slug'] ) )
866
973
$args->rewrite['slug'] = $post_type;
867
if ( !isset($args->rewrite['with_front']) )
974
if ( ! isset( $args->rewrite['with_front'] ) )
868
975
$args->rewrite['with_front'] = true;
976
if ( ! isset( $args->rewrite['pages'] ) )
977
$args->rewrite['pages'] = true;
978
if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive )
979
$args->rewrite['feeds'] = (bool) $args->has_archive;
869
981
if ( $args->hierarchical )
870
982
$wp_rewrite->add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
872
984
$wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
986
if ( $args->has_archive ) {
987
$archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive;
988
$wp_rewrite->add_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' );
989
if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) {
990
$feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
991
$wp_rewrite->add_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
992
$wp_rewrite->add_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
994
if ( $args->rewrite['pages'] )
995
$wp_rewrite->add_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' );
873
998
$wp_rewrite->add_permastruct($post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask);
894
1019
* Builds an object with all post type capabilities out of a post type object
896
* Accepted keys of the capabilities array in the post type object:
897
* - edit_post - The meta capability that controls editing a particular object of this post type. Defaults to "edit_ . $capability_type" (edit_post).
898
* - edit_posts - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts).
899
* - edit_others_posts - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s" (edit_others_posts).
900
* - publish_posts - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts).
901
* - read_post - The meta capability that controls reading a particular object of this post type. Defaults to "read_ . $capability_type" (read_post).
902
* - read_private_posts - The capability that controls reading private posts. Defaults to "read_private . $capability_type . s" (read_private_posts).
903
* - delete_post - The meta capability that controls deleting a particular object of this post type. Defaults to "delete_ . $capability_type" (delete_post).
1021
* Post type capabilities use the 'capability_type' argument as a base, if the
1022
* capability is not set in the 'capabilities' argument array or if the
1023
* 'capabilities' argument is not supplied.
1025
* The capability_type argument can optionally be registered as an array, with
1026
* the first value being singular and the second plural, e.g. array('story, 'stories')
1027
* Otherwise, an 's' will be added to the value for the plural form. After
1028
* registration, capability_type will always be a string of the singular value.
1030
* By default, seven keys are accepted as part of the capabilities array:
1032
* - edit_post, read_post, and delete_post are meta capabilities, which are then
1033
* generally mapped to corresponding primitive capabilities depending on the
1034
* context, which would be the post being edited/read/deleted and the user or
1035
* role being checked. Thus these capabilities would generally not be granted
1036
* directly to users or roles.
1038
* - edit_posts - Controls whether objects of this post type can be edited.
1039
* - edit_others_posts - Controls whether objects of this type owned by other users
1040
* can be edited. If the post type does not support an author, then this will
1041
* behave like edit_posts.
1042
* - publish_posts - Controls publishing objects of this post type.
1043
* - read_private_posts - Controls whether private objects can be read.
1045
* These four primitive capabilities are checked in core in various locations.
1046
* There are also seven other primitive capabilities which are not referenced
1047
* directly in core, except in map_meta_cap(), which takes the three aforementioned
1048
* meta capabilities and translates them into one or more primitive capabilities
1049
* that must then be checked against the user or role, depending on the context.
1051
* - read - Controls whether objects of this post type can be read.
1052
* - delete_posts - Controls whether objects of this post type can be deleted.
1053
* - delete_private_posts - Controls whether private objects can be deleted.
1054
* - delete_published_posts - Controls whether published objects can be deleted.
1055
* - delete_others_posts - Controls whether objects owned by other users can be
1056
* can be deleted. If the post type does not support an author, then this will
1057
* behave like delete_posts.
1058
* - edit_private_posts - Controls whether private objects can be edited.
1059
* - edit_published_posts - Controls whether published objects can be deleted.
1061
* These additional capabilities are only used in map_meta_cap(). Thus, they are
1062
* only assigned by default if the post type is registered with the 'map_meta_cap'
1063
* argument set to true (default is false).
1065
* @see map_meta_cap()
906
* @param object $args
1068
* @param object $args Post type registration arguments
907
1069
* @return object object with all the capabilities as member variables
909
1071
function get_post_type_capabilities( $args ) {
911
'edit_post' => 'edit_' . $args->capability_type,
912
'edit_posts' => 'edit_' . $args->capability_type . 's',
913
'edit_others_posts' => 'edit_others_' . $args->capability_type . 's',
914
'publish_posts' => 'publish_' . $args->capability_type . 's',
915
'read_post' => 'read_' . $args->capability_type,
916
'read_private_posts' => 'read_private_' . $args->capability_type . 's',
917
'delete_post' => 'delete_' . $args->capability_type,
1072
if ( ! is_array( $args->capability_type ) )
1073
$args->capability_type = array( $args->capability_type, $args->capability_type . 's' );
1075
// Singular base for meta capabilities, plural base for primitive capabilities.
1076
list( $singular_base, $plural_base ) = $args->capability_type;
1078
$default_capabilities = array(
1079
// Meta capabilities
1080
'edit_post' => 'edit_' . $singular_base,
1081
'read_post' => 'read_' . $singular_base,
1082
'delete_post' => 'delete_' . $singular_base,
1083
// Primitive capabilities used outside of map_meta_cap():
1084
'edit_posts' => 'edit_' . $plural_base,
1085
'edit_others_posts' => 'edit_others_' . $plural_base,
1086
'publish_posts' => 'publish_' . $plural_base,
1087
'read_private_posts' => 'read_private_' . $plural_base,
919
$labels = array_merge( $defaults, $args->capabilities );
920
return (object) $labels;
1090
// Primitive capabilities used within map_meta_cap():
1091
if ( $args->map_meta_cap ) {
1092
$default_capabilities_for_mapping = array(
1094
'delete_posts' => 'delete_' . $plural_base,
1095
'delete_private_posts' => 'delete_private_' . $plural_base,
1096
'delete_published_posts' => 'delete_published_' . $plural_base,
1097
'delete_others_posts' => 'delete_others_' . $plural_base,
1098
'edit_private_posts' => 'edit_private_' . $plural_base,
1099
'edit_published_posts' => 'edit_published_' . $plural_base,
1101
$default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
1104
$capabilities = array_merge( $default_capabilities, $args->capabilities );
1106
// Remember meta capabilities for future reference.
1107
if ( $args->map_meta_cap )
1108
_post_type_meta_capabilities( $capabilities );
1110
return (object) $capabilities;
1114
* Stores or returns a list of post type meta caps for map_meta_cap().
1119
function _post_type_meta_capabilities( $capabilities = null ) {
1120
static $meta_caps = array();
1121
if ( null === $capabilities )
1123
foreach ( $capabilities as $core => $custom ) {
1124
if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) )
1125
$meta_caps[ $custom ] = $core;
2040
2267
* Retrieve number of recent posts.
2270
* @uses wp_parse_args()
2045
* @param int $num Optional, default is 10. Number of posts to get.
2046
* @return array List of posts.
2273
* @param string $deprecated Deprecated.
2274
* @param array $args Optional. Overrides defaults.
2275
* @param string $output Optional.
2048
function wp_get_recent_posts($num = 10) {
2051
// Set the limit clause, if we got a limit
2054
$limit = "LIMIT $num";
2057
$sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status IN ( 'draft', 'publish', 'future', 'pending', 'private' ) ORDER BY post_date DESC $limit";
2058
$result = $wpdb->get_results($sql, ARRAY_A);
2060
return $result ? $result : array();
2278
function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
2280
if ( is_numeric( $args ) ) {
2281
_deprecated_argument( __FUNCTION__, '3.1', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) );
2282
$args = array( 'numberposts' => absint( $args ) );
2285
// Set default arguments
2287
'numberposts' => 10, 'offset' => 0,
2288
'category' => 0, 'orderby' => 'post_date',
2289
'order' => 'DESC', 'include' => '',
2290
'exclude' => '', 'meta_key' => '',
2291
'meta_value' =>'', 'post_type' => 'post', 'post_status' => 'draft, publish, future, pending, private',
2292
'suppress_filters' => true
2295
$r = wp_parse_args( $args, $defaults );
2297
$results = get_posts( $r );
2299
// Backward compatibility. Prior to 3.1 expected posts to be returned in array
2300
if ( ARRAY_A == $output ){
2301
foreach( $results as $key => $result ) {
2302
$results[$key] = get_object_vars( $result );
2304
return $results ? $results : array();
2307
return $results ? $results : false;
3934
4150
* 'gmt' is when the last post was modified in GMT time.
3939
4153
* @uses apply_filters() Calls 'get_lastpostmodified' filter
3941
4155
* @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
3942
4156
* @return string The date the post was last modified.
3944
4158
function get_lastpostmodified($timezone = 'server') {
3947
$add_seconds_server = date('Z');
3948
$timezone = strtolower( $timezone );
3950
$lastpostmodified = wp_cache_get( "lastpostmodified:$timezone", 'timeinfo' );
3951
if ( $lastpostmodified )
3952
return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone );
3954
switch ( strtolower($timezone) ) {
3956
$lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1");
3959
$lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1");
3962
$lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1");
4159
$lastpostmodified = _get_last_post_time( $timezone, 'modified' );
3966
4161
$lastpostdate = get_lastpostdate($timezone);
3967
4162
if ( $lastpostdate > $lastpostmodified )
3968
4163
$lastpostmodified = $lastpostdate;
3970
if ( $lastpostmodified )
3971
wp_cache_set( "lastpostmodified:$timezone", $lastpostmodified, 'timeinfo' );
3973
4165
return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone );
4169
* Retrieve latest post date data based on timezone.
4174
* @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
4175
* @param string $field Field to check. Can be 'date' or 'modified'.
4176
* @return string The date.
4178
function _get_last_post_time( $timezone, $field ) {
4181
if ( !in_array( $field, array( 'date', 'modified' ) ) )
4184
$timezone = strtolower( $timezone );
4186
$key = "lastpost{$field}:$timezone";
4188
$date = wp_cache_get( $key, 'timeinfo' );
4191
$add_seconds_server = date('Z');
4193
$post_types = get_post_types( array( 'publicly_queryable' => true ) );
4194
array_walk( $post_types, array( &$wpdb, 'escape_by_ref' ) );
4195
$post_types = "'" . implode( "', '", $post_types ) . "'";
4197
switch ( $timezone ) {
4199
$date = $wpdb->get_var("SELECT post_{$field}_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
4202
$date = $wpdb->get_var("SELECT post_{$field} FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
4205
$date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
4210
wp_cache_set( $key, $date, 'timeinfo' );
3977
4217
* Updates posts in cache.
3979
4219
* @usedby update_page_cache() Aliased by this function.
4724
4985
add_filter('the_preview', '_set_preview');
4990
* Returns the post's parent's post_ID
4994
* @param int $post_id
4996
* @return int|bool false on error
4998
function wp_get_post_parent_id( $post_ID ) {
4999
$post = get_post( $post_ID );
5000
if ( !$post || is_wp_error( $post ) )
5002
return (int) $post->post_parent;
5006
* Checks the given subset of the post hierarchy for hierarchy loops.
5007
* Prevents loops from forming and breaks those that it finds.
5009
* Attached to the wp_insert_post_parent filter.
5012
* @uses wp_find_hierarchy_loop()
5014
* @param int $post_parent ID of the parent for the post we're checking.
5015
* @parem int $post_ID ID of the post we're checking.
5017
* @return int The new post_parent for the post.
5019
function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) {
5020
// Nothing fancy here - bail
5021
if ( !$post_parent )
5024
// New post can't cause a loop
5025
if ( empty( $post_ID ) )
5026
return $post_parent;
5028
// Can't be its own parent
5029
if ( $post_parent == $post_ID )
5032
// Now look for larger loops
5034
if ( !$loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ) )
5035
return $post_parent; // No loop
5037
// Setting $post_parent to the given value causes a loop
5038
if ( isset( $loop[$post_ID] ) )
5041
// There's a loop, but it doesn't contain $post_ID. Break the loop.
5042
foreach ( array_keys( $loop ) as $loop_member )
5043
wp_update_post( array( 'ID' => $loop_member, 'post_parent' => 0 ) );
5045
return $post_parent;
5049
* Returns an array of post format slugs to their translated and pretty display versions
5053
* @return array The array of translations
5055
function get_post_format_strings() {
5057
'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard
5058
'aside' => _x( 'Aside', 'Post format' ),
5059
'chat' => _x( 'Chat', 'Post format' ),
5060
'gallery' => _x( 'Gallery', 'Post format' ),
5061
'link' => _x( 'Link', 'Post format' ),
5062
'image' => _x( 'Image', 'Post format' ),
5063
'quote' => _x( 'Quote', 'Post format' ),
5064
'status' => _x( 'Status', 'Post format' ),
5065
'video' => _x( 'Video', 'Post format' ),
5066
'audio' => _x( 'Audio', 'Post format' ),
5072
* Retrieves an array of post format slugs.
5076
* @return array The array of post format slugs.
5078
function get_post_format_slugs() {
5079
// 3.2-early: use array_combine() and array_keys( get_post_format_strings() )
5081
'standard' => 'standard', // Special case. any value that evals to false will be considered standard
5084
'gallery' => 'gallery',
5088
'status' => 'status',
5096
* Returns a pretty, translated version of a post format slug
5100
* @param string $slug A post format slug
5101
* @return string The translated post format name
5103
function get_post_format_string( $slug ) {
5104
$strings = get_post_format_strings();
5106
return $strings['standard'];
5108
return ( isset( $strings[$slug] ) ) ? $strings[$slug] : '';
5112
* Sets a post thumbnail.
5116
* @param int|object $post Post ID or object where thumbnail should be attached.
5117
* @param int $thumbnail_id Thumbnail to attach.
5118
* @return bool True on success, false on failure.
5120
function set_post_thumbnail( $post, $thumbnail_id ) {
5121
$post = get_post( $post );
5122
$thumbnail_id = absint( $thumbnail_id );
5123
if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
5124
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' );
5125
if ( ! empty( $thumbnail_html ) ) {
5126
update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
5134
* Returns a link to a post format index.
5138
* @param $format string Post format
5139
* @return string Link
5141
function get_post_format_link( $format ) {
5142
$term = get_term_by('slug', 'post-format-' . $format, 'post_format' );
5143
if ( ! $term || is_wp_error( $term ) )
5145
return get_term_link( $term );
5149
* Filters the request to allow for the format prefix.
5154
function _post_format_request( $qvs ) {
5155
if ( ! isset( $qvs['post_format'] ) )
5157
$slugs = get_post_format_slugs();
5158
if ( isset( $slugs[ $qvs['post_format'] ] ) )
5159
$qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ];
5160
$tax = get_taxonomy( 'post_format' );
5161
$qvs['post_type'] = $tax->object_type;
5164
add_filter( 'request', '_post_format_request' );
5167
* Filters the post format term link to remove the format prefix.
5172
function _post_format_link( $link, $term, $taxonomy ) {
5174
if ( 'post_format' != $taxonomy )
5176
if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
5177
return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
5179
$link = remove_query_arg( 'post_format', $link );
5180
return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
5183
add_filter( 'term_link', '_post_format_link', 10, 3 );
5186
* Remove the post format prefix from the name property of the term object created by get_term().
5191
function _post_format_get_term( $term ) {
5192
if ( isset( $term->slug ) ) {
5193
$term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
5197
add_filter( 'get_post_format', '_post_format_get_term' );
5200
* Remove the post format prefix from the name property of the term objects created by get_terms().
5205
function _post_format_get_terms( $terms, $taxonomies, $args ) {
5206
if ( in_array( 'post_format', (array) $taxonomies ) ) {
5207
if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {
5208
foreach( $terms as $order => $name ) {
5209
$terms[$order] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
5212
foreach ( (array) $terms as $order => $term ) {
5213
if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
5214
$terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
5221
add_filter( 'get_terms', '_post_format_get_terms', 10, 3 );
5224
* Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().
5229
function _post_format_wp_get_object_terms( $terms ) {
5230
foreach ( (array) $terms as $order => $term ) {
5231
if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
5232
$terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
5237
add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );
b'\\ No newline at end of file'