admin管理员组文章数量:1310185
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!!
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!!
Share Improve this question asked Jan 9, 2021 at 4:08 OoxOox 535 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 0After 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.
本文标签: searchsearching in custom meta field
版权声明:本文标题:search - searching in custom meta field 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741846617a2400817.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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? – Sally CJ Commented Jan 9, 2021 at 9:42publisher
) than using the post meta. Because taxonomies are better suited at grouping things together and taxonomy queries also run faster than meta queries. – Sally CJ Commented Jan 9, 2021 at 9:52echo $ 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. – Oox Commented Jan 9, 2021 at 18:14SELECT 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 – Oox Commented Jan 9, 2021 at 18:19