Display Custom Post Type name in search results

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Display Custom Post Type name in search results

This topic contains 2 replies, has 2 voices, and was last updated by ConnectionPoint ConnectionPoint 3 years, 11 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • #28330

    Hopefully a simple question/answer…

    I’m trying to display the name of a Custom Post Type in the search results by altering the Advanced Description Field and including a {custom_field_name}.

    For example, we have a number of CPT including Announcements, Articles, Case Studies, Product Notes, Trends & Insights, etc. Customers searching the site need to be able to identify the type of post to help them find the correct information they need.

    I’ve tried a number of options…

    wpv-post-type (from Toolset plugin) e.g. {wpv_post_type}: {descriptionfield}

    … but haven’t been able to guess at the correct name.

    Is there a shortcut name for the post type?

    Thanks, Daryl

    Ernest Marcinko
    Ernest Marcinko

    Hi Dary,


    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;
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    Works great – thx!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Display Custom Post Type name in search results’ is closed to new replies.