I am using Option 3 from this answer as my source. So far it is working great, it is splitting my search results by post type and listing the posts from each post type in their own sections. The only problem is that this solution does not appear to allow for hyperlinking the post titles using the_permalink();
How can I adapt this code so that I can also wrap the post titles in an anchor tag that will direct to the post?
<?php get_header(); ?>
<?php
$search_query = new WP_Query(
array(
'posts_per_page' => -1,
's' => esc_attr($_GET['s']),
'post_status' => 'publish'
)
);
if ($search_query->have_posts()) : ?>
<section id="search-results">
<div class="container">
<div class="row">
<div class="col">
<h2><?php echo $search_query->found_posts.' Search results found'; ?> for: "<?php echo get_search_query(); ?>"</h2>
</div>
</div>
<div class="search-results-list">
<?php
$types = array( 'post', 'page', 'glossary' );
$posts_titles = [];
while($search_query->have_posts()) {
$search_query->the_post();
$type = $search_query->post->post_type;
if (!isset($posts_titles[$type]))
$posts_titles[$type] = [];
$posts_titles[$type][] = get_the_title();
}
rewind_posts();
foreach($types as $type) :
if (!isset($posts_titles[$type]))
continue;
?>
<div class="row">
<h3>
<?php
$post_type_obj = get_post_type_object($type);
echo $post_type_obj->labels->name
?>
</h3>
</div>
<div class="row">
<div class="col">
<ul>
<?php foreach($posts_titles[$type] as $title) : ?>
<li class="search-item">
<a href="PERMALINK SHOULD GO HERE"><?php echo htmlspecialchars($title); ?></a>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</section>
<?php else:
echo '<div class="search-suggestions-no-results">
<p>' . __('Sorry, no results found', 'text-domain') . '</p>
</div>';
endif; ?>
<?php get_footer(); ?>