1. Home
  2. Knowledge Base
  3. Document Library Pro
  4. Advanced Usage

Code snippet: Display the file's upload date instead of the document creation date

The 'Date' column in Document Library Pro normally displays the date when the document post was added to your site. This may be different from the date when you uploaded the Media Library file which is attached to the document.

The following code snippet will modify the date field in the document library to display the upload date of the file which is attached to the document. If you're unsure how to use it, then read our article about code snippets or request a quote from our customization service.

add_filter('document_library_pro_data_date', function($date, $post) {
    if (!class_exists('Barn2\Plugin\Document_Library_Pro\Document')) {
        return $date;
    }

    if (!is_object($post) || !isset($post->ID)) {
        return $date;
    }

    try {
        $document = new Barn2\Plugin\Document_Library_Pro\Document($post->ID);
        $attachment_id = $document->get_file_id();

        if ($attachment_id) {
            $formatted_date = get_the_date('Y-m-d', $attachment_id);
            return $formatted_date ?: $date; // Fallback to original date if formatting fails
        }
    } catch (\Exception $e) {
        error_log('Document Library Pro date filter error: ' . $e->getMessage());
    }

    return $date;
}, 10, 2);

Related Articles

If searching the knowledge base hasn't answered your question, please contact support.