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!!
publisher()
function, you should just useget_post()
to get the current post orget_the_ID()
to get just the post ID), so are you 100% sure there arebooks
posts with the metapublisher
containingHarvard
/harvard
in the value? Tryecho $the_query->request;
and see if the SQL is good. And try adding'suppress_filters' => true
to your$args
just to see if a code is modifying your args - does suppressing filters return any results?publisher
) than using the post meta. Because taxonomies are better suited at grouping things together and taxonomy queries also run faster than meta queries.echo $ the_query-> reques
t because although the SQL query it does is fine, the point is that it is not looking at the correct table because I have a specific table for the custom fields.SELECT SQL_CALC_FOUND_ROWS wp_8_posts.ID FROM wp_8_posts INNER JOIN wp_8_postmeta ON ( wp_8_posts.ID = wp_8_postmeta.post_id ) WHERE 1=1 AND ( ( wp_8_postmeta.meta_key = 'publisher' AND wp_8_postmeta.meta_value LIKE '{}Harvard{}' ) ) AND wp_8_posts.post_type = 'books' AND ((wp_8_posts.post_status = 'publish')) GROUP BY wp_8_posts.ID ORDER BY wp_8_posts.post_date DESC LIMIT 0, 10
. My table with custom field is `wp_8_postmeta_books' See: link