Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Search results content › Reply To: Search results content
Thank you very much! I have tried, but I get a refused key message: https://i.imgur.com/pwZs7Fe.png
However, I didn’t need the SFTP access after all, I was able to create and test a custom code snippet that fetches the PDF attachments from the products and indexes them with the products as well. I have applied the custom code to the functions.php file in your theme directory.
The way it works is, that you can set up a widget to search for products. If there are any PDFs attached to the product, the code will check and will try to find it in the media library. If it’s in the media library, then it will index it’s contents with the product contents. Therefore searching parts from the PDF contents will return the product as the results as well. Just make sure that he attached PDF exists in the Media library as well.
The code that I applied below, for archive purposes:
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
);