Reply To: Include Atributes in search

#13554
fespigado
fespigado
Participant

As promised, I share the code to show the stock in the search results if it is greater than 0. thanks for your help

add_filter( ‘asp_results’, ‘asp_stock_to_content’, 1, 1 );

function asp_stock_to_content( $results ) {
$custom_field = “_stock”;

foreach ($results as $k=>$v) {
if ($v->content_type != “pagepost” || $v->post_type != “product”)
continue;
if ( function_exists(‘get_field’) )
$meta_value = get_field( $v->id, $custom_field, true ); // ACF support
else
$meta_value = get_post_meta( $v->id, $custom_field, true );
// Modify the post title to add the meta value

if ( $meta_value > 0)
$results[$k]->content .= ‘<br>Stock: ‘ . intval($meta_value) . ‘‘;
else
continue;
}

return $results;
}