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 2 years, 8 months ago.
- AuthorPosts
- July 8, 2020 at 9:02 pm #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…
__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
July 9, 2020 at 1:54 pm #28340Hi 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!
Best,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 :)
July 10, 2020 at 4:20 am #28358Works great – thx!
- AuthorPosts
You must be logged in to reply to this topic.