WordPress.org

Make WordPress Core

Opened 2 weeks ago

Closed 2 weeks ago

Last modified 2 weeks ago

#48382 closed defect (bug) (duplicate)

do_action convert array of post to single post object if array contain only 1 post

Reported by: fabienlege Owned by:
Milestone: Priority: normal
Severity: normal Version: trunk
Component: Plugins Keywords:
Focuses: Cc:
PR Number:

Description

in wp-includes/plugin.php line 456, function do_action automaticaly convert array of one post to single post object.

code to reproduce bug :

<?php
$posts = get_post($args); // this return an array on 0,1 or more wp_post objects.
do_action('test',$posts);
add_action('test',function($posts){
    var_dump($posts); // if $posts is empty array, we have an empty array. if its an array of multiple posts, we have an array of multiple posts. but, if an array of one post is passed to parameter, we have a single wp_post object (not in an array).
})

It's a real problem if you need to do action on get_posts result. you don't know if the result is 0, 1 or more post and, forexemple, a foreach fail

Change History (2)

#1 @dlh
2 weeks ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hi @fabienlege, and thanks for filing this report. This issue is being tracked in #47438.

#2 @SergeyBiryukov
2 weeks ago

  • Component changed from General to Plugins
Note: See TracTickets for help on using tickets.