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

WordPress Trac wp-trac at lists.automattic.com
Wed Jun 29 19:13:40 UTC 2011


#17935: function get_post( integer ) bypasses posts_* filters
----------------------------+-----------------------------
 Reporter:  postpostmodern  |      Owner:
     Type:  defect (bug)    |     Status:  new
 Priority:  normal          |  Milestone:  Awaiting Review
Component:  Query           |    Version:  3.2
 Severity:  normal          |   Keywords:
----------------------------+-----------------------------
 Calling get_post( integer ) bypasses all normal query filters, like
 `posts_fields` and `posts_join`.  I ran into this problem when using Quick
 Edit on wp-admin/edit.php in conjunction with query filters in a plugin.

 Example plugin:

 {{{

 /*
 Plugin Name: get_posts bug report
 */

 add_filter( 'posts_fields', 'get_post_bug_posts_fields' );
 add_filter( 'posts_request', 'get_post_bug_posts_request' );
 add_filter( 'manage_posts_columns', 'get_post_bug_manage_posts_columns' );
 add_filter( 'manage_posts_custom_column',
 'get_post_bug_manage_posts_custom_column', 10, 2 );

 function get_post_bug_posts_fields( $sql ){
         $sql .= ", 'ffffoooooooo' AS `foo`, 'baaaaaarrrr' AS `bar`";
         return $sql;
 }

 function get_post_bug_posts_request( $sql ){
         //var_dump( $sql );
         return $sql;
 }

 function get_post_bug_manage_posts_columns( $headings ){
         $a = array_slice( $headings, 0, 3 );
         $b = array_slice( $headings, 4 );

         $headings = array_merge( $a, array('foo'=>'Foo'),
 array('bar'=>'Bar'), $b );
         return $headings;
 }

 function get_post_bug_manage_posts_custom_column( $column_name, $post_id
 ){
         global $post;

         switch( $column_name ){
                 case 'foo':
                         echo $post->foo;
                         break;

                 case 'bar':
                         echo $post->bar;
                         break;
         }
 }
 }}}

 On page load, the edit list table will load correctly with two custom
 columns, however saving the post from the Quick Edit interface will result
 in the two columns having no data.

 Proposed fix:

 wp-includes/post.php line 391

 {{{
 $_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE
 ID = %d LIMIT 1", $post_id));
 }}}

 becomes

 {{{
 $_posts = query_posts( array('p' => $post_id,
                              'post_type' => 'any',
                              'post_status' => 'any',
                              'posts_per_page' => 1) );
 $_post = count( $_posts ) ? $_posts[0] : $wpdb->get_row(
 $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1",
 $post_id) );
 }}}

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


More information about the wp-trac mailing list