searching in custom meta field

I have multiples custom field in a post type. i can save data, update and display, all fine. now I’m doing the search engine. as it is a library, it must be possible to search in specific fields (custom field), publisher, country, etc. Here the code I have for publisher…

i open a variable

    function register_query_vars( $vars ) {
    $vars[] = 'publisher'; //
    return $vars;
} 
add_filter( 'query_vars', 'register_query_vars' );

search form by publisher in shortcode

function publisher_search() {
    add_shortcode( 'search_form_publisher', 'search_form_publisher' );
}
add_action( 'init', 'publisher_search' );

form:

function search_form_publisher( $atts ){
 
    $output="<form action="" . esc_url( home_url() ) . '" method="get" role="search">';
    $output .= '<input type="text" name="publisher" value="' . get_search_query() . '" class="field" placeholder="Search..."/>';
    $output .= '<input type="hidden" name="post_type" value="books" />';
    $output .= '<input type="submit" value="Search!" class="button" />';
    $output .= '</form>';
    return $output;
}

so far, all good. when searching, the following url is built:

http://localhost/library/?publisher=search_term&post_type=books

which uses the archive-{post-type}.php template

$args = array(
  'post_type'      => 'books',
  'post_status'    => 'publish',
  'meta_query'     => array(
    array(
      'key'     => 'publisher',
      'value'   => get_query_var('publisher'),
      'compare' => 'LIKE',

    ) 
  )
);

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        // Your code here

   publisher();
       
    endwhile;

} else {
        echo 'no posts found';
}
/* Restore original Post Data */
wp_reset_postdata();

I print the value of the custom field publisher like this, its work well:

function publisher() {
    global $wp_query;
    $post = $wp_query->post;
    $publisher = get_post_meta($post->ID, 'publisher', true);
    if (!empty($publisher)) {
        echo $publisher;
    }
}

The problem is, however, that the query does not return any results. the print is this:

Array ( [post_type] => books [post_status] => publish [meta_query] => Array ( [0] => Array ( [key] => publisher [value] => Harvard [compare] => LIKE ) ) )

that is, if it collects the value of the search term. I have tried many codes that I have found on the internet but none of them work. I think that a function that adds a posts_search filter, but the ones I have tried do not work.

I would appreciate your help to resolve the issue.I’m lost. Thank you!!

1 Answer
1

After thinking about the problem, I found the simplest solution, do a direct query using $wpdb

in archive-{post-type}.php

<?php 

global $wpdb;
$meta_key = get_query_var('publisher');
$result = $wpdb->get_results("
    SELECT  * 
    FROM  " . $wpdb->prefix. "postmeta_books 
    WHERE publisher LIKE  '%$meta_key%' 
");
foreach( $result as $results ) {

        echo $results->publisher;
        echo '<br/>';
        

    }
?>

It is the basic query, it needs to be refined but I wanted to share it to close the question. of course work very well.

Leave a Comment