WordPress.org

Make WordPress Core

Ticket #12526: has_term.diff

File has_term.diff, 6.1 KB (added by ptahdunbar, 4 years ago)

introduces has_term, deprecates in_category in favor of has_category, and has_category and has_tag are wrappers for has_term

  • wp-includes/category-template.php

     
    230230        return apply_filters( 'the_category', $thelist, $separator, $parents ); 
    231231} 
    232232 
    233  
    234233/** 
    235  * Check if the current post in within any of the given categories. 
    236  * 
    237  * The given categories are checked against the post's categories' term_ids, names and slugs. 
    238  * Categories given as integers will only be checked against the post's categories' term_ids. 
    239  * 
    240  * Prior to v2.5 of WordPress, category names were not supported. 
    241  * Prior to v2.7, category slugs were not supported. 
    242  * Prior to v2.7, only one category could be compared: in_category( $single_category ). 
    243  * Prior to v2.7, this function could only be used in the WordPress Loop. 
    244  * As of 2.7, the function can be used anywhere if it is provided a post ID or post object. 
    245  * 
    246  * @since 1.2.0 
    247  * 
    248  * @uses is_object_in_term() 
    249  * 
    250  * @param int|string|array $category. Category ID, name or slug, or array of said. 
    251  * @param int|post object Optional.  Post to check instead of the current post. @since 2.7.0 
    252  * @return bool True if the current post is in any of the given categories. 
    253  */ 
    254 function in_category( $category, $_post = null ) { 
    255         if ( empty( $category ) ) 
    256                 return false; 
    257  
    258         if ( $_post ) { 
    259                 $_post = get_post( $_post ); 
    260         } else { 
    261                 $_post =& $GLOBALS['post']; 
    262         } 
    263  
    264         if ( !$_post ) 
    265                 return false; 
    266  
    267         $r = is_object_in_term( $_post->ID, 'category', $category ); 
    268         if ( is_wp_error( $r ) ) 
    269                 return false; 
    270         return $r; 
    271 } 
    272  
    273 /** 
    274234 * Display the category list for the post. 
    275235 * 
    276236 * @since 0.71 
     
    969929} 
    970930 
    971931/** 
    972  * Check if the current post has any of given tags. 
     932 * Check if the current post has any of given categories. 
    973933 * 
    974  * The given tags are checked against the post's tags' term_ids, names and slugs. 
    975  * Tags given as integers will only be checked against the post's tags' term_ids. 
    976  * If no tags are given, determines if post has any tags. 
     934 * @since 3.0.0 
    977935 * 
    978  * Prior to v2.7 of WordPress, tags given as integers would also be checked against the post's tags' names and slugs (in addition to term_ids) 
    979  * Prior to v2.7, this function could only be used in the WordPress Loop. 
    980  * As of 2.7, the function can be used anywhere if it is provided a post ID or post object. 
     936 * @uses is_object_in_term() 
     937 * @see has_term() 
    981938 * 
     939 * @param string|int|array $tag Optional. The category name/category_id/slug or array of them to check for. 
     940 * @param int|post object Optional.  Post to check instead of the current post. 
     941 * @return bool True if the current post has any of the the given categories (or any category, if no category specified). 
     942 */ 
     943function has_category( $category = '', $_post = null ) { 
     944        return has_term( $category, 'category', $_post ); 
     945} 
     946 
     947/** 
     948 * Check if the current post has any of given tags. 
     949 * 
    982950 * @since 2.6.0 
    983951 * 
    984952 * @uses is_object_in_term() 
     953 * @see has_term() 
    985954 * 
    986  * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for. 
    987  * @param int|post object Optional.  Post to check instead of the current post. @since 2.7.0 
     955 * @param string|int|array $tag Optional. The tag name/tag_id/slug or array of them to check for. 
     956 * @param int|post object Optional.  Post to check instead of the current post. 
    988957 * @return bool True if the current post has any of the the given tags (or any tag, if no tag specified). 
    989958 */ 
    990959function has_tag( $tag = '', $_post = null ) { 
    991         if ( $_post ) { 
     960        return has_term( $tag, 'post_tag', $_post ); 
     961} 
     962 
     963 
     964/** 
     965 * Check if the current post has any of given terms. 
     966 * 
     967 * The given terms are checked against the post's terms' term_ids, names and slugs. 
     968 * Terms given as integers will only be checked against the post's terms' term_ids. 
     969 * If no terms are given, determines if post has any terms. 
     970 * 
     971 * @since 3.0.0 
     972 * 
     973 * @uses is_object_in_term() 
     974 * 
     975 * @param string|int|array $tag Optional. The term name/term_id/slug or array of them to check for. 
     976 * @param int|post object Optional.  Post to check instead of the current post. @since 2.7.0 
     977 * @return bool True if the current post has any of the the given tags (or any tag, if no tag specified). 
     978 */ 
     979function has_term( $term = '', $taxonomy = '', $_post = null ) { 
     980        if ( $_post ) 
    992981                $_post = get_post( $_post ); 
    993         } else { 
     982        else 
    994983                $_post =& $GLOBALS['post']; 
    995         } 
    996984 
    997985        if ( !$_post ) 
    998986                return false; 
    999987 
    1000         $r = is_object_in_term( $_post->ID, 'post_tag', $tag ); 
     988        $r = is_object_in_term( $_post->ID, $taxonomy, $term ); 
    1001989        if ( is_wp_error( $r ) ) 
    1002990                return false; 
    1003991        return $r; 
  • wp-admin/includes/deprecated.php

     
    162162        return unregister_setting( $option_group, $option_name, $sanitize_callback ); 
    163163} 
    164164 
     165/** 
     166 * Check if the current post in within any of the given categories. 
     167 * 
     168 * The given categories are checked against the post's categories' term_ids, names and slugs. 
     169 * Categories given as integers will only be checked against the post's categories' term_ids. 
     170 * 
     171 * Prior to v2.5 of WordPress, category names were not supported. 
     172 * Prior to v2.7, category slugs were not supported. 
     173 * Prior to v2.7, only one category could be compared: in_category( $single_category ). 
     174 * Prior to v2.7, this function could only be used in the WordPress Loop. 
     175 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object. 
     176 * 
     177 * @since 1.2.0 
     178 * @deprecated 3.0.0 
     179 * @deprecated Use has_category() 
     180 * @see has_category() 
     181 * 
     182 * @uses is_object_in_term() 
     183 * 
     184 * @param int|string|array $category. Category ID, name or slug, or array of said. 
     185 * @param int|post object Optional.  Post to check instead of the current post. @since 2.7.0 
     186 * @return bool True if the current post is in any of the given categories. 
     187 */ 
     188function in_category( $category, $_post = null ) { 
     189        _deprecated_function( __FUNCTION__, '3.0', 'has_category()' ); 
     190        return has_category( $category, $_post ); 
     191} 
    165192?> 
     193 No newline at end of file