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 10, 2025 at 3:23 pm #53242
erika
ParticipantThank you so much Ernest, really appreciate this code! I think I may not be getting the config quite right with it. Sorry, i did not say the key is loaded under “erik” and not your name.
I set “Index table option” to “Products” only (not attachment) and I “create a new index”. Under my search for a keyword, the results return the pdf that contains that keyword but does not have the excerpts. I notice that the pdf under media does not have “Content (not editable)” like the directly indexed pdfs. Also, I cannot set the remote server when choosing “Products” only – unlike “Attachment” that has the remote server set up.
Should I choose “Attachment” as well? In fact, this message to you may be redundant. I have selected attachment as well and am currently reindexing. It takes awhile though so I wrote this message.
March 10, 2025 at 4:17 pm #53243erika
ParticipantThanks Ernest! Appreciate it! I wrote a response earlier and I am not sure where it is. So, please see the attachment called search.jpg. The search for “Chernobyl” correctly returns the product item with the attached pdf and the attachment with the excerpt. How can I get the product item to return the excerpt in the pdf indexed content?
With the public key, I forgot to mention to use my name “erik”.
March 11, 2025 at 1:02 pm #53263Ernest Marcinko
KeymasterHi!
Well, that will need another bit of custom code for sure, as the media file ID needs to be fetched, but I think it’s possible. Try the snippet below. Make sure that the custom code I provided in my previous reply is also active, it contains a function that is needed for this code as well:
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 = wd_substr_at_word($field_value, 130); break; } } } } } return $results; }March 11, 2025 at 1:18 pm #53264erika
ParticipantThanks again Ernest! Do I need both settings as attached? As per your code, the pdf gets retrieved with the product – however, does it get sent to the remote server without the attachment option?
March 11, 2025 at 1:21 pm #53266Ernest Marcinko
KeymasterYes – you need both of those settings and the files have to be uploaded to the media library, otherwise the plugin can’t access it (it has to have a valid ID in the database).
So the best course of action is to uplod the file via the Media menu, then on the product editor add it from the library. I think when you only upload it via the product editor, it may not be registered in the media library.
March 11, 2025 at 1:41 pm #53267erika
ParticipantI am doing something wrong, see attached. The product is item 1 in the search result – however it has no excerpts (I did index with the remote server option). Item 2 in the search I assume is the pdf directly accessed (not through the product). I need the product only with the excerpt and not the pdf yet. The user needs to go to the product with add to cart – which is working correctly from the search result. Thanks Ernest, I appreciate the help very much.
March 11, 2025 at 1:42 pm #53269erika
Participanthttps://realacademy.net/the-knowing-field/ test with “Hutus”
March 11, 2025 at 1:55 pm #53270erika
ParticipantMarch 11, 2025 at 2:11 pm #53273erika
ParticipantThe content has definitely been added to the attachment details, attached.
March 11, 2025 at 3:49 pm #53278Ernest Marcinko
KeymasterAll right, the setup seems to be okay, all you need the the excerpt.
Make sure that this custom code is also applied with the previous ones. I can’t check as I can only see the development environment:
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 = wd_substr_at_word($field_value, 130); break; } } } } } return $results; }This fetches the attachment text and replaces it in the live product results.
-
This reply was modified 1 year, 2 months ago by
Ernest Marcinko.
March 11, 2025 at 6:12 pm #53290erika
ParticipantHi Ernest, the code below is what’s in my production functions.php. It is not returning the text/content for the search results unfortunately – I did a recreate index. See attached.
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 = wd_substr_at_word($field_value, 130); 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 11, 2025 at 6:19 pm #53293erika
ParticipantYou cannot access this content.
March 11, 2025 at 7:11 pm #53294Ernest Marcinko
KeymasterI see. On the screenshot however it seems to be the first 130characters of the PDF content though, so that is correct. I guess you want like the context around the search keyword? For that maybe try this variation, but I’m not sure if that’s even possible:
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, 130, 9999999, $args['s'], explode($args['s'])); break; } } } } } return $results; }March 12, 2025 at 10:48 am #53298erika
ParticipantThanks Ernest, by context I mean the same as what appears in the results with a search against a pdf. With the new code there is an error in the dropdown, please see attached. You are welcome to try ftp in. the code is under /data/realacademy…
March 12, 2025 at 11:16 am #53300Ernest Marcinko
KeymasterI see, I’m not sure if that’s possible, but let’s try to correct the code on site.
Can you please check the back-end access details? I tried to log-in but username/pass is incorrect. Thank you!
-
This reply was modified 1 year, 2 months ago by
-
AuthorPosts
- You must be logged in to reply to this topic.