[wp-trac] [WordPress Trac] #57131: get_the_terms(): Parameter #1 ($post) of type is nullable.

WordPress Trac noreply at wordpress.org
Fri Nov 18 00:25:08 UTC 2022


#57131: get_the_terms(): Parameter #1 ($post) of type is nullable.
-----------------------------------+------------------------------
 Reporter:  omaeyusuke             |       Owner:  (none)
     Type:  enhancement            |      Status:  new
 Priority:  normal                 |   Milestone:  Awaiting Review
Component:  Taxonomy               |     Version:  3.6
 Severity:  normal                 |  Resolution:
 Keywords:  has-patch 2nd-opinion  |     Focuses:  docs
-----------------------------------+------------------------------

Comment (by omaeyusuke):

 Replying to [comment:2 SergeyBiryukov]:
 Thank you fast review!

 > Looking at the other taxonomy functions, including
 get_the_category_list(), in_category(), the_category(), has_category(),
 has_term(), etc., it seems like we don't generally include null as an
 accepted value for the optional $post or $post_id parameter, but we do
 note "Defaults to the current post".
 Yes! IMO $post is required. I see that my document comment was worded
 inconsistently. I'll fix it!

 > I'm not quite sure about this change. While technically `$post` can be
 `null` here, passing the ID explicitly as the current documentation
 suggests seems more readable and future-proof:
 > {{{
 > $term = get_the_terms( get_the_ID(), 'category' );
 > }}}

 `get_the_ID()` is false if the post does not exist. Therefore, the
 following statement is strictly inconsistent from the documentation of
 get_the_terms().

 {{{#!php
 <?php
 $term = get_the_terms( get_the_ID() /* false if not non-existent post. */,
 'category' );
 }}}

 Also, since you are accepting int, there is no option not to use
 `get_post()` inside the function to check for non-existent posts, then I
 see no thin ...... reason not to accept nullable.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/57131#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list