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 2 years, 1 month ago.

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

    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…

    __name
    __post
    __post_name
    __post_type
    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

    #28340
    Ernest Marcinko
    Ernest Marcinko
    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;
    }
    Best,
    Ernest Marcinko

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


    #28358
    ConnectionPoint
    ConnectionPoint
    Participant

    Works great – thx!

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

You must be logged in to reply to this topic.