[wp-trac] [WordPress Trac] #17935: function get_post( integer ) bypasses posts_* filters

WordPress Trac wp-trac at lists.automattic.com
Wed Jun 29 23:35:56 UTC 2011


#17935: function get_post( integer ) bypasses posts_* filters
----------------------------+------------------------------
 Reporter:  postpostmodern  |       Owner:
     Type:  defect (bug)    |      Status:  reopened
 Priority:  normal          |   Milestone:  Awaiting Review
Component:  Query           |     Version:  3.2
 Severity:  normal          |  Resolution:
 Keywords:                  |
----------------------------+------------------------------

Comment (by postpostmodern):

 Hi, thanks for taking another look at this. Im glad you took this ticket -
 having written http://scribu.net/wordpress/custom-sortable-columns.html,
 may I use that code for reference?

 In your example, price_column_display() makes a call to get_post_meta() on
 every row.

 However, if you had expected that every post on your site would have a
 price, and it you would always want to display it, you might to have
 wanted to always join the postmeta table and select this data along with
 the post.

 {{{
 function price_posts_fields( $sql ){
         global $wpdb;
         $sql .= ", {$wpdb->postmeta}.meta_value AS `price`";
         return $sql;
 }
 add_filter( 'posts_fields', 'price_posts_fields' );

 function price_posts_join( $sql ){
         global $wpdb;
         $sql .= " LEFT JOIN {$wpdb->postmeta}
                                 ON {$wpdb->posts}.ID =
 {$wpdb->postmeta}.post_id
                                 AND {$wpdb->postmeta}.meta_key = 'price'
 ";
         return $sql;
 }
 add_filter( 'posts_join', 'price_posts_join' );
 }}}

 Then, price_column_display() becomes

 {{{
 function price_column_display( $column_name, $post_id ) {
         global $post;
         echo trim($post->price) ? $post->price : __( 'undefined', 'my-
 plugin' );
 }
 }}}

 This has the benefit of having price as part of $post site wide.  In my
 development, I am using comparable functions to join geolocation data to
 each post, so $post->latitude and $post->longitude is globally available,
 including for display in the Edit table.

 As far as calling both get_posts() and get_post(), there seems to be a
 problem when using the Quick Edit on a draft, as far as I can tell, around
 line 2643 in wp-includes/query.php, $post_status_obj->protected is TRUE on
 drafts, and ends up returning an empty array.  Perhaps there is another
 query variable I should be using?

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/17935#comment:5>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list