Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Search results content › Reply To: Search results content
March 11, 2025 at 6:12 pm
#53290
Participant
Hi 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
);