admin - Access $post object when adding custom columns to list table

We've got a custom post type called 'products' which stores all of its additional meta data in a separate

We've got a custom post type called 'products' which stores all of its additional meta data in a separate table for performance reasons (we're expecting 1000s of records). We have extended the query performed on the 'Products' list table page in the admin area to include all the additional meta data in the resulting 'posts' object (eg. product_supplier, product_featured etc).

The issue is that we want to add some additional columns to the list table WITHOUT performing additional DB queries to lookup the data (since it's already in our 'posts' object mentioned above). We would usually use the filter 'manage_product_posts_custom_column' to define the custom column content but this is only passed the $post_id rather than the $post object (which has all the info we need without additional queries).

The way I see it we either need to make the $post object a global variable so we can access it from within the 'manage_product_posts_custom_column' filter or we need to, somehow, add addition column methods (which are passed the $item object as as such would have the data). This second method could use a custom List Table class is suppose but we would only want to extend the WP_Posts_List_Table to add the addition column methods. The issue with that is I don't know how to do it...

Hopefully this all makse sense and someone out there will have an idea of how to achieve this.

Cheers,

Will

We've got a custom post type called 'products' which stores all of its additional meta data in a separate table for performance reasons (we're expecting 1000s of records). We have extended the query performed on the 'Products' list table page in the admin area to include all the additional meta data in the resulting 'posts' object (eg. product_supplier, product_featured etc).

The issue is that we want to add some additional columns to the list table WITHOUT performing additional DB queries to lookup the data (since it's already in our 'posts' object mentioned above). We would usually use the filter 'manage_product_posts_custom_column' to define the custom column content but this is only passed the $post_id rather than the $post object (which has all the info we need without additional queries).

The way I see it we either need to make the $post object a global variable so we can access it from within the 'manage_product_posts_custom_column' filter or we need to, somehow, add addition column methods (which are passed the $item object as as such would have the data). This second method could use a custom List Table class is suppose but we would only want to extend the WP_Posts_List_Table to add the addition column methods. The issue with that is I don't know how to do it...

Hopefully this all makse sense and someone out there will have an idea of how to achieve this.

Cheers,

Will

Share Improve this question asked May 28, 2020 at 8:47 Will FairhurstWill Fairhurst 134 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You don't need a global $post object, WP gives the column callback the post ID, which is all that is needed.

Looking at this question/answer, we see this code:

add_filter( 'manage_book_posts_columns', 'set_custom_edit_book_columns' );
function set_custom_edit_book_columns($columns) {
....
    $columns['publisher'] = __( 'Publisher', 'your_text_domain' );

    return $columns;
}

add_action( 'manage_book_posts_custom_column' , 'custom_book_column', 10, 2 );
function custom_book_column( $column, $post_id ) {
    switch ( $column ) {
....
        case 'publisher' :
            echo get_post_meta( $post_id , 'publisher' , true ); 
            break;

    }
}

Notice that $post_id was then used to retrieve post meta, and if you needed a post object, $current_post = get_post( $post_id ) can be used.

Generally you don't need to rely on global variables, particularly $post, almost all APIs take a post ID, and every field on the post variable is available as a filterable API function. e.g. get_the_title() etc

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742394490a4435668.html

相关推荐

  • admin - Access $post object when adding custom columns to list table

    We've got a custom post type called 'products' which stores all of its additional meta data in a separate

    2天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信