我的网站是 woocommerce 网站。我的问题是搜索框。每次我使用SKU搜索时都没有结果。它仅在我搜索帖子,产品和页面结果时显示结果。它没有显示woocommerce SKU产品。
如何修复此WordPress搜索结果?
这是我的WordPress search.php
<?php get_header(); ?>
<div id="main-content">
<div class="container">
<div id="content-area" class="clearfix">
<div id="left-area" >
<?php
global $wpdb;
$s = (isset($_GET['s']) && $_GET['s']) ? urldecode($_GET['s']) : false;
if($s){
$where = ' title LIKE \'%' . esc_sql( like_escape($s) ) . '%\'';
$arr_title = explode(' ',$s);
foreach($arr_title as $search_term):
$where .= ' OR title LIKE \'%' . esc_sql( like_escape( $search_term ) ) . '%\'';
endforeach;
$query = "
SELECT *
FROM ( SELECT p.ID as id, p.post_title as title, p.post_name as slug, p.post_excerpt as excerpt, 'post' AS type FROM $wpdb->posts AS p WHERE p.post_type IN ('post', 'page', 'product') AND p.post_status = 'publish'
UNION ALL
SELECT t.term_id as id, t.name as title, t.slug as slug, tx.description as excerpt, 'term' AS type FROM $wpdb->terms AS t LEFT JOIN $wpdb->term_taxonomy AS tx ON t.term_id = tx.term_id WHERE tx.taxonomy NOT IN ('product_tag', 'post_tag')
) AS tb WHERE $where ";
//start pagination
$results = $wpdb->get_results($query);
$total = count($results);
$paged = (isset($_GET['paged'])) ? $_GET['paged'] : 1;
$limit = 50;
//end pagination
$offset = $paged ? ($paged - 1) * $limit : 0;
$results = $wpdb->get_results( $query . " LIMIT $offset, $limit ");
if ($results) :
foreach( $results as $row ) :
$url_img = false; $permalink = false;
if($row->type=='post'){
// $url_img = wp_get_attachment_url( $attachment_id, 'small');
// echo 'post_id= ' . $row->ID . ', att_id= ' . (int)$attachment_id . '<br>';
$result = wp_get_attachment_image_src (get_post_thumbnail_id($row->id), 'medium');
$url_img = $result['url'];
$permalink = get_permalink($row->id);
}
if($row->type=='term'){
$permalink = get_bloginfo('url').'/products/'.$row->slug;
}
?>
<article id="post-<?php echo $row->id; ?>" class="et_pb_post post-<?php echo $row->id; ?>">
<?php if($url_img): ?>
<a href="<?php echo $permalink; ?>"><img src="<?php echo $url_img; ?>" alt="<?php echo $row->title; ?>" /> </a>
<?php endif; ?>
<h2 class="entry-title"><a href="<?php echo (!is_wp_error($permalink)) ? $permalink : '#'; ?>"><?php echo $row->title; ?></a></h2>
<?php echo $row->excerpt; ?>
<div class="read-more-link-container"><a href="<?php echo (!is_wp_error($permalink)) ? $permalink : '#'; ?>" class="read-more-link"> Read More</a></div>
</article>
<?php endforeach;
if($total > $limit):
$pl_args = array(
'base' => add_query_arg('paged','%#%'),
'format' => '',
'total' => ceil($total / $limit),
'current' => max(1, $paged),
);
echo paginate_links($pl_args);
endif;
else : get_template_part( 'includes/no-results', 'index' );
endif;
}else{
get_template_part( 'includes/no-results', 'index' );
}
?>
</div> <!-- #left-area -->
<?php get_sidebar('search'); ?>
</div> <!-- #content-area -->
</div> <!-- .container -->
</div> <!-- #main-content -->
<?php get_footer(); ?>