Change search to outpout category name instead of Post type name
-
Im using a ajax search which is great, however i would like to change it to show custom post type categorys instead of custom post type. I added the taxonomy for the custom post type categorys, and works great, but te titel is still “portfolio” which is the CPT, and i want the titels to be the categorys. The code:
if (!function_exists('sf_ajaxsearch')) { function sf_ajaxsearch() { $search_term = trim($_POST['s']); $search_query_args = array( 's' => $search_term, 'post_type' => 'any', 'portfolio-category' => 'fullvids', 'post_status' => 'publish', 'suppress_filters' => false, 'numberposts' => -1 ); $search_query_args = http_build_query($search_query_args); $search_results = get_posts( $search_query_args ); $count = count($search_results); $shown_results = 5; $search_results_ouput = ""; if (!empty($search_results)) { $sorted_posts = $post_type = array(); foreach ($search_results as $search_result) { $sorted_posts[$search_result->post_type][] = $search_result; // Check we don't already have this post type in the post_type array if (empty($post_type[$search_result->post_type])) { // Add the post type object to the post_type array $post_type[$search_result->post_type] = get_post_type_object($search_result->post_type); } } $i = 0; foreach ($sorted_posts as $key => $type) { $search_results_ouput .= '<div class="search-result-pt">'; if(isset($post_type[$key]->labels->name)) { $search_results_ouput .= "<h6>".$post_type[$key]->labels->name."</h6>"; } else if(isset($key)) { $search_results_ouput .= "<h6>".$key."</h6>"; } else { $search_results_ouput .= "<h6>".__("Other", "swiftframework")."</h6>"; } foreach ($type as $post) { $img_icon = ""; $post_format = get_post_format($post->ID); if ( $post_format == "" ) { $post_format = 'standard'; } $post_type = get_post_type($post->ID); if ($post_type == "post") { if ($post_format == "quote" || $post_format == "status") { $img_icon = "fa-quote-left"; } else if ($post_format == "image") { $img_icon = "fa-picture-o"; } else if ($post_format == "chat") { $img_icon = "fa-comments-o"; } else if ($post_format == "audio") { $img_icon = "fa-music"; } else if ($post_format == "video") { $img_icon = "fa-film"; } else if ($post_format == "link") { $img_icon = "fa-link"; } else { $img_icon = "fa-pencil"; } } else if ($post_type == "product") { $img_icon = "fa-shopping-cart"; } else if ($post_type == "portfolio") { $img_icon = "fa-picture-o"; } else if ($post_type == "team") { $img_icon = "fa-user"; } else if ($post_type == "galleries") { $img_icon = "fa-picture-o"; } else { $img_icon = "fa-file"; } $post_title = get_the_title($post->ID); $post_permalink = get_permalink($post->ID); $image = get_the_post_thumbnail( $post->ID, 'thumbnail' ); $search_results_ouput .= '<div class="search-result">'; if ($image) { $search_results_ouput .= '<div class="search-item-img"><a href="'.$post_permalink.'">'.$image.'</div>'; } else { $search_results_ouput .= '<div class="search-item-img"><a href="'.$post_permalink.'" class="img-holder"><i class="'.$img_icon.'"></i></a></div>'; } $search_results_ouput .= '<div class="search-item-content">'; $search_results_ouput .= '<h5><a href="'.$post_permalink.'">'.$post_title.'</a></h5>'; $search_results_ouput .= '</div>'; $search_results_ouput .= '</div>'; $i++; if ($i == $shown_results) break; } $search_results_ouput .= '</div>'; if ($i == $shown_results) break; } if ($count > 1) { $search_results_ouput .= '<a href="'.get_search_link($search_term).'" class="all-results">'.sprintf(__("View all %d results", "swiftframework"), $count).'</a>'; } } else { $search_results_ouput .= '<div class="no-search-results">'; $search_results_ouput .= '<h6>'.__("No results", "swiftframework").'</h6>'; $search_results_ouput .= '<p>'.__("No search results could be found, please try another query.", "swiftframework").'</p>'; $search_results_ouput .= '</div>'; } echo $search_results_ouput; die(); } add_action('wp_ajax_sf_ajaxsearch', 'sf_ajaxsearch'); add_action('wp_ajax_nopriv_sf_ajaxsearch', 'sf_ajaxsearch'); } if (!function_exists('sf_ajaxurl')) { function sf_ajaxurl() { ?> <script type="text/javascript"> var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>'; </script> <?php } add_action('wp_head','sf_ajaxurl'); } ?>
- The topic ‘Change search to outpout category name instead of Post type name’ is closed to new replies.