[wp-trac] [WordPress Trac] #48382: do_action convert array of post to single post object if array contain only 1 post

WordPress Trac noreply at wordpress.org
Mon Oct 21 11:52:02 UTC 2019


#48382: do_action convert array of post to single post object if array contain only
1 post
--------------------------+-----------------------------
 Reporter:  fabienlege    |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  trunk
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 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
 <?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

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/48382>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list