我创建了一个自定义模板,用于搜索我们的产品,其中包含大量过滤器和一个输入字段,可以搜索字符。但是,它只能使用一个或两个字符,如“a”,“ag”,但不能超过两个字符。如果我搜索“pol”或“polar”之类的东西,它会将我重定向到WordPress 404 - PAGE NOT FOUND。
更新
我尝试了两个解决方案:一个使用自定义查询ajax,并且搜索工作正常,但我遇到的唯一问题是分页;另一个使用WordPress标准查询对象,它适用于分页,但我遇到了搜索字符长度的问题。以下是我尝试的两次尝试。
尝试#1 :使用Ajax。这段代码写在function.php文件中
function get_search_results()
{
global $wpdb;
if(!empty($_POST['search_input'])) {
$search_input = $_POST['search_input'];
$where = 'pm.meta_value LIKE '. "'%".$search_input."%'".' AND pm.meta_key LIKE "%pattern_number"';
$where1 = 'p.post_title LIKE '."'%".$search_input."%'".' AND p.post_type = "product"';
$search_sql = 'SELECT DISTINCT(pm.post_id), ph.lightness, ph.color_label FROM wp_posts p
INNER JOIN wp_products_huecolor ph on p.ID = ph.post_id
INNER JOIN wp_postmeta pm ON p.ID = pm.post_id
WHERE p.post_status = "publish" AND p.post_type = "product" AND (('.$where.') OR ('.$where1.')) ORDER BY ph.color_label, ph.lightness ASC';
$total_query = "SELECT COUNT(1) FROM (${search_sql}) AS combined_table";
$total = $wpdb->get_var( $total_query );
$items_per_page = 10;
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$offset = ( $page * $items_per_page ) - $items_per_page;
$page = $wpdb->get_results( $search_sql.' LIMIT '.$offset.', '.$items_per_page );
//echo "SQL = ".$wpdb->last_query; wp_die();
echo '<ul>';
foreach ($page as $object)
{
// get custom fields values related to a post by post id
$fields = get_fields($object->post_id);
// get post details by post id
$post_detail = get_post($object->post_id, $object );
$brand_name = $fields[brand]->name;
$full_sheet_image = $fields[full_sheet_image][sizes][thumbnail];
$post_title = $post_detail->post_title;
$post_name = $post_detail->post_name;
?>
<li class="post-pattern">
<div class="search-productsBoxSecond">
<a target="_blank" href="<?php echo site_url( '/products/'.$post_name.'', 'http' ); ?>"><img src="<?php echo $full_sheet_image; ?>" alt="" class="vc_single_image-img attachment-full"></a>
<div class="search-productNameSecond"><a target="_blank" href="<?php echo site_url( '/products/'.$post_name.'', 'http' ); ?>"><?php echo $post_title; ?></a></div>
<div class="search-productBrandSecond"><?php echo $brand_name; ?></div>
</div>
</li>
<?php
}
echo '</ul>';
echo '<div class="search-product-navigation">';
echo paginate_links( array(
'base' => add_query_arg( 'cpage', '%#%', site_url( '/pattern-search/' ) ),
'format' => '',
'prev_text' => __('«'),
'next_text' => __('»'),
'total' => ceil($total / $items_per_page),
'current' => $page
));
echo '</div>';
}
exit();
}
add_action('wp_ajax_nopriv_filter_featured_products','filter_featured_products');
add_action('wp_ajax_filter_featured_products','filter_featured_products');
尝试#2 :使用自定义模板中的标准WP_Query。
<?php
$args = array();
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$search_keywords = ( get_query_var( 's' ) ) ? get_query_var( 's' ) : "";
if(isset($search_keywords) && $search_keywords != ''){
$args = array(
'post_type' => 'product',
'search_prod_title' => $search_keywords,
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged,
'wildcard_on_key' => true,
'meta_query' => array(
array(
'key' => 'product_type_%_pattern_number',
'value' => $search_keywords,
'compare' => 'LIKE',
),
)
);
}else{
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged
);
}
// query for getting featured product
add_filter( 'posts_fields', 'products_posts_select_filter', 10, 2 );
add_filter( 'posts_join', 'products_posts_join_filter', 10, 2 );
add_filter( 'posts_where', 'title_filter', 10, 2 );
add_filter( 'posts_orderby', 'products_posts_orderby', 10, 2 );
add_filter('posts_groupby', 'products_posts_groupby', 10, 2);
$sql_query = new WP_Query( $args );
//echo $wpdb->last_query; die();
//echo '<pre>'; print_r($sql_query); die;
//echo $sql_query->request; die;
remove_filter( 'posts_fields', 'products_posts_select_filter', 10, 2 );
remove_filter( 'posts_join', 'products_posts_join_filter', 10, 2 );
remove_filter( 'posts_where', 'title_filter', 10, 2 );
remove_filter( 'posts_orderby', 'products_posts_posts_orderby', 10, 2 );
remove_filter('posts_groupby', 'products_posts_groupby', 10, 2);
if( $sql_query->have_posts()):
echo '<ul>';
while( $sql_query->have_posts()): $sql_query->the_post();
{
//get custom fields meta values related to a post
$fields = get_fields($post->ID);
$full_sheet_image = $fields[full_sheet_image][sizes][thumbnail];
//echo'<pre>'; print_r($post_detail); die;
?>
<li class="post-pattern">
<div class="search-productsBoxSecond">
<a target="_blank" href="<?php echo site_url( '/products/'.$post->post_name.'', 'http' ); ?>"><img src="<?php echo $full_sheet_image; ?>" alt="" class="vc_single_image-img attachment-full"></a>
<div class="search-productNameSecond"><a target="_blank" href="<?php echo site_url( '/products/'.$post->post_name.'', 'http' ); ?>"><?php echo $post->post_title; ?></a></div>
<div class="search-productBrandSecond"><?php echo $fields[brand]->name; ?></div>
</div>
</li>
<?php
}
endwhile; ?>
</ul>
<!-- pagination -->
<div class="search-product-navigation">
<?php
echo paginate_links(array(
'total' => $sql_query->max_num_pages
));
?>
</div>
<?php endif; ?>
这里是函数.php
中编写的过滤函数/*Replaced meta_key = with meta_key LIKE */
add_filter( 'posts_where', function ( $where, \WP_Query $q )
{
// Check for our custom query var
if ( true !== $q->get( 'wildcard_on_key' ) )
return $where;
// Lets filter the clause
$where = str_replace( 'meta_key =', 'meta_key LIKE', $where );
return $where;
}, 10, 2 );
function title_filter( $where, &$wp_query )
{
global $wpdb;
if ( $search_term = $wp_query->get( 'search_prod_title' ) ) {
$post_title_clause = '('.$wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $search_term ) ) . '%\') OR';
$where = substr_replace($where, $post_title_clause , 6, 0);
}
return $where;
}
function products_posts_select_filter( $fields, &$wp_query ){
$fields = 'DISTINCT(wp_posts.ID), wp_posts.post_title, wp_posts.post_name, ph.lightness, ph.color_label';
return $fields;
}
function products_posts_join_filter( $join, &$wp_query ){
$join .= " INNER JOIN wp_products_huecolor ph on wp_posts.ID = ph.post_id";
return $join ;
}
function products_posts_orderby( $orderby, &$wp_query ){
$orderby = 'ph.color_label, ph.lightness ASC';
return $orderby;
}
function products_posts_groupby( $groupby, &$wp_query ){
return $groupby = '';
}