0

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>
0

1 Answer 1

0

The default WordPress search query only works on post_title and post_content fields from the wp_posts table, not including meta stored on the wp_postmeta table.

It really depends on what type of search you want to perform how you could solve this, for instance, "full text"-type search, require a match on a given post meta, custom ranking, etc.

I think the current good practice, and a simple solution, would be to add those kind of meta data using blocks (which would save their data on the post_content field), and also saving them as meta data (as "source of truth" or more advanced querying).

For showing the meta data, you can use get_post_meta( $post->ID, 'meta_key', true ) or echo $post->my_custom_meta_key (WordPress would automatically show the value from the given meta key).

2
  • Thank you, this answer was really useful. But don't you think there may be other solutions than putting the custom fields' metadata into the post content blocks? It would be really nice to find a more elegant fix for this. Oct 19, 2022 at 8:38
  • I found a solution for this: adambalee.com/… Oct 19, 2022 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.