Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Display Custom Post Type name in search results › Reply To: Display Custom Post Type name in search results
July 9, 2020 at 1:54 pm
#28340
Keymaster
Hi Dary,
Hi!
There is no custom variable for that unfortunately, but still should be possible to achieve via custom code.
In the advanced title or content fields add this as the variable (__post_type) – note the different parenthesis, it is neccessary.
Then add this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!
add_filter( 'asp_results', 'asp_custom_cpt_results', 10, 1 );
function asp_custom_cpt_results( $results ) {
// Parse through each result item
foreach ($results as $k=>&$r) {
if ( $r->content_type == 'pagepost' ) {
$post_type_obj = get_post_type_object( $r->post_type );
$r->content = str_replace('(__post_type)', $post_type_obj->labels->singular_name, $r->content);
$r->title = str_replace('(__post_type)', $post_type_obj->labels->singular_name, $r->title);
}
}
return $results;
}