Skip to main contentSkip to footer

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.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Indexing the post ID
Are post ID’s searchable in Relevanssi? No. Relevanssi does understand the WP_Query p parameter to restrict the search by post…
Ajax Load More

…counts as a new search for Relevanssi logs. If you don’t like that, you can make Relevanssi only log the first Ajax Load More search. If you want that, use these functions instead of the one above: add_filter( ‘alm_modify_query_args’, function( $args ) { global $rlv_is_offset_query; $args[‘relevanssi’] = true; if (……global $rlv_is_offset_query; $args[‘relevanssi’] = true; if ( $args[‘offset’] ) { $rlv_is_offset_query = true; } return $args; } ); add_filter( ‘relevanssi_ok_to_log’, function( $ok, $query ) { global $rlv_is_offset_query; if ( $rlv_is_offset_query ) { $ok = false; } return $ok; }, 10, 2 ); Now Relevanssi logs each search query only once….

Related Posts:

Comment Section:

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed