[wp-hackers] Idea to enhance the WP_Post class

Mike Schinkel mike at newclarity.net
Fri Apr 5 05:50:47 UTC 2013


On Apr 5, 2013, at 1:43 AM, Funkatron <funkatronic at gmail.com> wrote:
> This idea is a simple one: encapsulating the post-based template tags
> (the_ID, the_title, the_content, etc) and make them methods of the WP_Post
> object.
> 
> Basically what this does is allow developers and theme makers to take
> advantage of the template tags in custom loops without actually messing
> around with the main loop or the global $post and removing the need to
> reset post data. Since the template tags use post data to begin, it makes
> sense for the core logic to be in the WP_Post class. The global template
> tags can just call the methods of the global $post object in a main loop.
> 
> For example if you have a custom query:
> 
> $query = new WP_Query();
> 
> Instead of calling the_post to load the global variables, you could do:
> 
> while( $query->have_posts ) : $query->next_post();
>    $query->post->the_title();
>    $query->post->the_content();
> endwhile;

+1, except this would be even better, uses __get():

while( $query->have_posts() ) : $query->next_post();
   $query->post->title;
   $query->post->content;
endwhile;

Or both could be supported for those who dislike overhead of __get().

-Mike


More information about the wp-hackers mailing list