Get WPFTS Pro today with 25% discount!

[Solved] Indexing and Search files by content in BuddyDrive


  • Hey, if you are using the BuddyPress system, should be you're also using BuddyDrive to store user files. Simple and nice file storage system, right. But there is a small problem is the impossibility to search those files by content.

    Here in this small article, I going to explain to you how to make this search happen in just a dozen lines of the PHP code!

    First of all, let's check the wp_posts table to understand how BuddyPress stores uploaded files. Each new file makes another record in wp_posts where post_type is equal to buddydrive-file and post_status is equal to buddydrive_public. Also, it feels like the file URL is stored in the wp_posts.guid column.

    Well, quite not standard, but nothing too complex for WPFTS Pro.
    Let's create some code to make that type of records understandable for the WPFTS indexer.

    add_filter('wpfts_index_post', function($index, $post)
    {
        global $wpfts_core;
    
        if ($post && $wpfts_core) {
            if ($post->post_type == 'buddydrive-file') {
                $url = $post->guide;
                if (strlen($url) > 0) {
                    // We need to include WPFTS Utils library to extract file content
                    require_once $wpfts_core->root_dir.'/includes/wpfts_utils.class.php';
    
                    // Extract data from the file by given URL (assuming this URL is mapped to local filesystem!)
                    $ret = WPFTS_Utils::GetCachedFileContent_ByLocalLink($url);
    
                    // Store extracted text (if present) into the specific index cluster
                    $index['attachment_content'] = isset($ret['post_content']) ? trim($ret['post_content']) : '';
                }
            }	
        }
    
        return $index;
    }, 3, 2);
    

    That's all? No, unfortunately. The indexer understands BuddyDrive-specific records, but WP_Query() still has no idea how to search them.
    So let's add another hook.

    add_action('pre_get_posts', function(&$wpq)
    {
        // We going to modify parameters only if we are in the main query and it's a text search
        if ($wpq->is_main_query() && $wpq->is_search) {
    	
            // We going to add buddydrive files to the search results only if attachment search 
            // is enabled (and thus, "inherit" and "attachments" are already in the list)
            if (in_array('inherit', $wpq->query_vars['post_status'])) {
                $wpq->query_vars['post_status'][] = 'buddydrive_public';
            }
            if (in_array('attachment', $wpq->query_vars['post_type'])) {
                $wpq->query_vars['post_type'][] = 'buddydrive-file';
            }
    
        }
    }, 20);
    

    Okay, and now that's all?

    Yes! Now you need to rebuild the index and then you can see the BuddyDrive searchable in the frontend search, wow!

    But you still can see one problem there. If you click on the search result, it redirects you to nowhere, because WordPress still not understand what is the link to the "single page" of the BuddyDrive "post" record.

    Let's make it clearer! As we investigated above, BuddyPress stores the file URL in the guid column.

    add_filter('wpfts_se_titlelink', function($r1, $post)
    {
        if ($post && ($post['post_type'] == 'buddydrive-file')) {
            $r1['link'] = $post['guid'];
        }
    
        return $r1;
    }, 10, 2);
    

    Okay! Now it finally works.

    Something not yet clear? Keep the community posted.

    If you just want the ready code for your website, download it:
    wpfts-addon-buddydrive-1.0.2.zip

Suggested Topics

  • 0 Votes
    5 Posts
    869 Views
    G
    @EpsilonAdmin Thanks for quick response and question. Yes, my hope was to use the standard WP search widget - but I haven't explored any other option. If there's a better way I'm happy to get guidance My site has been recently re-created in WordPress after quite a few years of running under Joomla and that Joomla installation had a free plugin called jiFiles (?) which did the document scanning/indexing. A standard search, scoped on file name or a string from within file content, would pull up a list of file names each hyperlinked to the file itself to easily click on for in-browser viewing (or possibly downloading). I appreciate that there are other WordPress plugins that offer a full document management system but they have a much larger feature set than I need and are also majorly expensive for a small non-profit community web site.
  • 0 Votes
    1 Posts
    587 Views
    No one has replied
  • Inconsistency in search result

    Bugs and Fixes search not found inconsistency
    2
    0 Votes
    2 Posts
    718 Views
    EpsilonAdminE
    Hi, @Amine The WPFTS Pro version you are using is very outdated. Please consider upgrading to 2.46.x which is the latest. It has a completely new indexing and searching algorithm and it should solve your issue.
  • 1 Votes
    1 Posts
    968 Views
    No one has replied
  • Searching data attached to image files

    Recipes and Known Solutions
    2
    1 Votes
    2 Posts
    1k Views
    EpsilonAdminE
    You can upload images to the WP Media Library as well and add the image description to the image. WPFTS Pro can search these images by the description then. Alternatively, you can create a special post type and upload images as "Featured Image" to each of these posts, and put the description and special information to the post_content or additional meta field(s) - depending on the number of data fields and type of information. In both cases, WPFTS Pro will help you with the search.

Be the first to read the news!

We are always improving our products, adding new functions and fixes. Subscribe now to be the first to get the updates and stay informed about our sales! We are not spammy. Seriously.

Join Us Now!

We are a professional IT-team. Many of us have been working in a Web IT field for more than 10 years. Our advanced experience of software development has been employed in the creation of the WordPress FullText Search plugin. All solutions implemented into the plugin have been used for 5 or more years in over 60 different web-projects.

We are looking forward to your comments, requests and suggestions in relation to the current plugin and future updates.

ewm-logo-450

The forum powered by NodeBB | Contributors