@pettera
It is the same post. But as you can see tthe second post is displayed with the category name, and the category slug at the end of the url.
[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 inwp_posts
wherepost_type
is equal to buddydrive-file andpost_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