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 badges1 Answer
Reset to default 1You 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