1

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

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

| improve this answer | |
  • Thanks Tom. I'm trying to avoid using get_post_meta as it requires another DB query which will, degrade performance significantly as the number of products gets into the 1000s. The get_post option might work well though. Will revert. Cheers – Will Fairhurst May 28 at 9:20
  • That's not true, when WordPress fetches a post, it also fetches all its post meta in advance and stores it in WP_Cache. That way post meta gets cached in memory for that request, and WP avoids lots of little DB queries on get_post_meta calls. If you've got a persistent object cache set up such as a Redis or Memcache dropin, then it's even faster as it'll fetch the post object and its post meta from RAM avoiding the database completely ( I strongly recommend object caches for massive performance gains ) – Tom J Nowell May 28 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.