Display Table by Category in Document Library Pro Plugin from a URL Parameter

Document Library Pro Plugin for WordPress offers a robust solution for managing and showcasing documents on your website. However, sometimes you may need to refine document displays based on specific categories using specific links. In this guide, we’ll delve into how you can filter documents by category using URL parameters, leveraging a simple PHP code snippet.

The following code can be added in your child theme’s functions.php file or using a snippets plugin, such as “WPCode“:

add_filter( 'document_library_pro_query_args', function( $args, $posts_table ) {
    if (isset($_GET['doc_cat'])) {
        $category = intval($_GET['doc_cat']); // Sanitize input to prevent SQL injection
        $args['tax_query'] = [
            array(
                'taxonomy' => 'doc_categories', // Taxonomy for document categories
                'field' => 'id',
                'terms' => array( $category ), 
                'operator' => 'IN',
            )
        ];
    }
    return $args;
}, 10, 2 );

Understanding the Code

Let’s dissect the code:

  • It hooks into the document_library_pro_query_args filter to modify the query arguments for retrieving documents.
  • When a URL parameter named doc_cat is detected, it extracts the provided category ID and sanitizes it using intval() to prevent SQL injection.
  • The code then enhances the query arguments ($args) to include a taxonomy query targeting the ‘doc_categories’ taxonomy, which corresponds to your document categories.
  • Finally, it returns the modified query arguments.

Utilizing the Functionality

To employ this feature, append ?doc_cat=<category_id> to the URL of the page containing the Document Library Pro shortcode. Replace <category_id> with the desired category’s actual ID.

For instance, if your Document Library Pro shortcode resides on a page with the URL https://yoursite.com/documents, and you wish to showcase documents from category ID 5, visit https://yoursite.com/documents?doc_cat=5.

Conclusion

By following these steps and incorporating the provided PHP code snippet into your WordPress website, you can seamlessly filter documents by category using URL parameters within the Document Library Pro Plugin. This customization empowers you to dynamically refine document displays, catering precisely to your audience’s preferences.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top