Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Search results content
- This topic has 34 replies, 2 voices, and was last updated 1 year, 2 months ago by
erika.
-
AuthorPosts
-
March 12, 2025 at 1:06 pm #53301
erika
ParticipantYou cannot access this content.
March 12, 2025 at 2:09 pm #53308Ernest Marcinko
KeymasterThank you!
I think I actually got it. There was a tiny mistake in the last code snippet, I fixed it, now it seems to show the context correctly. Please note that this is not perfect, but it should work in most cases. I can’t guarantee anything though, as this is beyond normal support, but I think it should work all right.
For future reference, the complete current solution is:
add_filter( 'asp_results', 'asp_get_attachment_to_product_content', 10, 4 ); function asp_get_attachment_to_product_content( $results, $search_id, $is_ajax, $args ) { // --- DO NOT CHANGE ANYTHING BELOW --- foreach ( $results as $k =>&$r ) { if ( $r->post_type !== 'product' ) { continue; } $product = wc_get_product( $r->id); if ( $product->is_downloadable() ) { // Loop through WC_Product_Download objects foreach ( $product->get_downloads() as $key_download_id => $download ) { $download_link = $download->get_file(); // File Url $id = asp_get_file_id_from_url($download_link); if ( $id !== '' ) { $field_value = get_post_meta( $id, '_asp_attachment_text', true ); if ( $field_value !== '' ) { $r->content = \WPDRMS\ASP\Utils\Str::getContext($field_value, 200, 9999999, $args['s'], explode(' ', $args['s'])); break; } } } } } return $results; } function asp_get_file_id_from_url($file_url) { $file_path = ltrim(str_replace(wp_upload_dir()['baseurl'], '', $file_url), '/'); global $wpdb; $statement = $wpdb->prepare("SELECT <code>ID</code> FROM <code>wp_posts</code> WHERE guid='%s';", $file_url, $file_path); $attachment = $wpdb->get_col($statement); if (count($attachment) < 1) { return ''; } return $attachment[0]; } add_filter( 'asp_post_content_before_tokenize', function ( $content, $post ) { if ( $post->post_type !== 'product' ) { return $content; } $product = wc_get_product( $post->ID ); if ( $product->is_downloadable() ) { // Loop through WC_Product_Download objects foreach ( $product->get_downloads() as $key_download_id => $download ) { $download_link = $download->get_file(); // File Url $id = asp_get_file_id_from_url($download_link); var_dump($id, $download_link); if ( $id !== '' ) { $field_value = get_post_meta( $id, '_asp_attachment_text', true ); if ( $field_value !== '' ) { $content .= ' ' . $field_value; } } } } return $content; }, 10, 2 );March 12, 2025 at 3:21 pm #53314erika
ParticipantThank you, it is working. I very much appreciate the extra support!! This will help us very much.
Will you integrate this code into future versions of the plugin or do I need to update functions.php with the code if I do a restore on my side?
March 13, 2025 at 8:44 am #53317Ernest Marcinko
KeymasterYou are very welcome!
This I probably can’t integrate, as it would require a lot of settings to configure correctly.
Keep the code snippets, they will work even after update. The best place to have them is by using the Code Snippets plugin. It keeps them active inbetween updates as well.
March 13, 2025 at 3:33 pm #53333erika
Participantgreat, will do.
-
AuthorPosts
- You must be logged in to reply to this topic.