I am working on a custom search engine for a custom post type (articles) with custom fields in WordPress PHP (online magazine). It works almost fine, the only problem is that somehow the search is only performed on the post titles. I want the search engine to run through all custom fields of the articles (title, subtitle, credits, article content etc.). And I also want to show some of these custom fields in the search results. How may I achieve this? the_content() and the_category() are returning NULL. Thank you in advance for any help.
functions.php:
/**
* Custom search for custom post type
*/
function template_chooser($template)
{
global $wp_query;
$post_type = get_query_var('post_type');
if ($wp_query->is_search && $post_type == 'articles') {
return locate_template('archive-search.php'); // redirect to archive-search.php
}
return $template;
}
add_filter('template_include', 'template_chooser');
archive-search.php
<?php
/* Template Name: Custom Search */
get_header(); ?>
<div class="search-result-container">
<div id="search-result-content">
<h3>Keresési eredmények: <?php echo htmlentities($s, ENT_QUOTES, 'UTF-8'); ?> </h3>
<?php if (have_posts()) : while (have_posts()) : the_post();
?>
<div id="post-<?php the_ID(); ?>" class="posts-wrapper">
<article class="search-result-post">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<h4 class="search-result-title">
<?php the_title(); ?>
</h4>
</a>
<span class="post-meta">
Dátum: <?php echo date('Y. m. j.'); ?>
</span>
</article><!-- #post -->
</div>
<?php endwhile; ?>
<?php endif; ?>
searchform.php:
<form role="search" action="<?php echo site_url('/'); ?>" method="get" class="search-form d-flex" id="search-form" accept-charset="utf-8">
<input type="search" id="search-bar" name="s" placeholder="Keresés..." minlength="2" required />
<input type="hidden" name="post_type" value="articles" id="articles" /> <!-- // hidden 'articles' value -->
<button class="search-icon" type="submit" name="submit" value="Search">
<i class="fa fa-search"></i>
</button>
</form>