This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: Search results content

#53294
Ernest MarcinkoErnest Marcinko
Keymaster

I 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;
}