Posted on

FileBird: Filtering attachments by folder

FileBird is one of many WordPress plugins that provide more control over the attachments in the Media Library. You can use the FileBird folders in Relevanssi searches to filter attachments based on their folder. Here’s a simple function you can add to your site:

add_filter( 'relevanssi_hits_filter', function( $hits ) {
    global $wpdb;
    if ( isset( $_REQUEST['folder'] ) ) {
        $folder = (int) $_REQUEST['folder'];
        $files  = $wpdb->get_col( "SELECT attachment_id FROM {$wpdb->prefix}fbv_attachment_folder WHERE folder_id = $folder" );

        $hits[0] = array_filter(
            $hits[0],
            function( $hit ) use ( $files ) {
                return in_array( $hit->ID, $files, true );
            }
        );
    }
    return $hits;
} );

Once you add this, you can use the folder parameter to restrict the search to a specific folder. The search ?s=search+term&folder=42 would only return attachments that are in folder 42. It’s possible to build more complex filtering, too; this should give you an idea of how the folder information can be used.

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

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