[wp-trac] [WordPress Trac] #12955: Add get_post filter

WordPress Trac noreply at wordpress.org
Fri Nov 18 11:01:19 UTC 2016


#12955: Add get_post filter
------------------------------------+-----------------------------
 Reporter:  JohnLamansky            |       Owner:
     Type:  feature request         |      Status:  new
 Priority:  normal                  |   Milestone:  Future Release
Component:  Posts, Post Types       |     Version:
 Severity:  normal                  |  Resolution:
 Keywords:  has-patch dev-feedback  |     Focuses:
------------------------------------+-----------------------------

Comment (by MikeSchinkel):

 Replying to [comment:48 danieliser]:
 > @MikeSchinkel, @jsphpl - That is not entirely true. In the technical
 aspect you are correct, but in execution the idea is doable.
 > Though you cannot extend WP_Post you can build a custom wrapper.

 Actually, ''technically speaking,'' you are absolutely correct.  It is
 completely technically possible to create a custom wrapper function.

 The problem is that -- '''practically speaking''' -- you cannot actually
 use it in a typical WordPress theme.

  Try the following; add this to `functions.php` in the your theme:

 {{{#!php
 <?php
 class Custom_Wrapper {
         private $_post;
         function __construct( $post ) {
                 $this->_post = $post;
         }
         function __set( $field, $value ) {
                 $this->$field = $value;
         }
         function __get( $field ) {
                 return $this->$field;
         }
         function custom_value() {
                 return 'It worked!';
         }
 }
 }}}

 Next create a `page.php` template and put the following code in it:

 {{{#!php
 <?php
 global $post;
 $post = new Custom_Wrapper( $post );
 the_post();
 echo $post->custom_value();
 }}}


 Now visit any page's URL.

 '''And therein lies the frustration with this seven (7) year old
 ticket.'''

 Replying to [comment:49 flixos90]:
 > in such a case awrapper classes could also do the trick.

 See the above example as to why your statement ignores the most common
 use-case for `$post`; in a theme that uses template tags where the
 template tags call `get_post()` to ''sanitize'' the global `$post`
 variable and cause the wrapped class instance to be thrown away.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/12955#comment:50>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list