[wp-trac] [WordPress Trac] #30970: setup_postdata( $post_id ) should still work

WordPress Trac noreply at wordpress.org
Sat Jan 10 01:57:59 UTC 2015


#30970: setup_postdata( $post_id ) should still work
-------------------------------+-----------------------------
 Reporter:  sc0ttkclark        |      Owner:
     Type:  enhancement        |     Status:  new
 Priority:  normal             |  Milestone:  Awaiting Review
Component:  Posts, Post Types  |    Version:  1.5
 Severity:  normal             |   Keywords:
  Focuses:                     |
-------------------------------+-----------------------------
 You should have to get_post before calling setup_postdata, the function
 (and by extension $wp_query->setup_postdata) should be able to accept an
 ID.

 Currently, setup_postdata() expects a full WP_Post (or faux standard post)
 object.

 Something as easy as this could allow for this to function:


 {{{
         public function setup_postdata( $post ) {
                 global $id, $authordata, $currentday, $currentmonth,
 $page, $pages, $multipage, $more, $numpages;

                 if ( ! is_object( $post ) ) {
                         $post = get_post( $post );

                         if ( ! $post ) {
                                 return false;
                         }
                 }

                 $id = (int) $post->ID;
 }}}

 This also has a fallback with a return false, which sticks with the bool
 typed return already documented.

 It solves cases where you may want to use WP template tags but all you
 have is a Post ID from another variable or from an option / transient /
 cached object.

 This can allow for 3 additional tests to be added to test setup_postdata(
 $post_id ) will return true and setup the post, setup_postdata(
 $null_or_anything_invalid ) will return false, and setup_postdata( $post )
 will return true and setup the post as normal with little to no overhead
 or ill effects.

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


More information about the wp-trac mailing list