Make WordPress Core

Opened 6 years ago

Last modified 6 years ago

#45273 new defect (bug)

get_postmeta: Inconsistent behavior of the 'single'-argument

Reported by: josk79's profile josk79 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Options, Meta APIs Keywords: 2nd-opinion
Focuses: Cc:

Description

The phpdoc of the get_postmeta-function in meta.php mentions that the single-argument has no effect if meta_key is not specified.

This is not true when the get_{$meta_type}_metadata-filter is used. If this filter returns an array and single is true only the first item of the array is returned. This not the expected behavior when meta_key is empty.

In my opinion this part:

        $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
        if ( null !== $check ) {
                if ( $single && is_array( $check ) ) {
                        return $check[0];
                }
                else {
                        return $check;
                }
        }

should be replace by:

        $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
        if ( null !== $check ) {
                if ( $meta_key && $single && is_array( $check ) ) {
                        return $check[0];
                }
                else {
                        return $check;
                }
        }

(Notice how I added $meta_key && )

NOTE: This is a breaking change in the rare occasion that get_{$meta_type}_metadata-filter is used in combination with an empty meta_key and single == true. If a fix is not desired; phpdoc must be changed to document this inconsistent behavior.

Change History (2)

#1 @SergeyBiryukov
6 years ago

  • Component changed from General to Options, Meta APIs

#2 @pento
6 years ago

  • Version trunk deleted
Note: See TracTickets for help on using tickets.