Wordpress 版本:3.5.2 Woocommerce 版本:2.0.13
以下是给出错误的代码。代码是产品搜索。尽管该函数按标签读取搜索,但它没有。它根据从复选框和下拉菜单中获取的其他标准进行搜索。在下面的代码中,我提到了错误开始的行,并在其旁边添加注释。
add_filter('the_posts', 'search_by_product_tag');
function search_by_product_tag($posts, $query = false)
{
//var_dump($_GET);
//$new_posts = array();
if($_GET['sh']=='search')
{
$search_tags = array(
'destination' => $_GET['destination'],
'days' => $_GET['days'],
'budget' => $_GET['budget']
);
//var_dump($search_tags);die();
$ignoreIds = array(0);
foreach($posts as $post)
{
$ignoreIds[] = $post->ID;
}
$matchedTags = get_post_by_tag($search_tags, $ignoreIds);
//var_dump($matchedTags);
if ($matchedTags)
{
foreach($matchedTags as $product_id)
{
$posts[] = get_posts($product_id->object_id);
}
}
return $posts;
}
if($_GET['f']=='filter')
{
//$filter_tags = array(
if(isset($_GET['price'])){
$price_range = $_GET['price'];
$price = $price_range;
}
else{
$price = "";
}
if(isset($_GET['holiday'])){
$holiday_type = $_GET['holiday'];
$holiday = implode("','", $holiday_type);
}
else{
$holiday = "";
}
if(isset($_GET['tour'])){
$tour_category = $_GET['tour'];
$tour = implode("','", $tour_category);
}
else{
$tour = "";
}
/*$ignoreIds = array(0);
foreach($posts as $post)
{
$ignoreIds[] = $post->ID;
}*/
$new_post = array();
//$matchedTags = get_post_by_filter_tag($price_range,$holiday_type,$tour_category, $ignoreIds);
$matchedTags = get_post_by_filter_tag($price,$holiday,$tour, $ignoreIds);
//var_dump($matchedTags);die();
if ($matchedTags)
{
foreach($matchedTags as $product_id)
{
$new_post[] = get_posts($product_id->ID); //Error was given after I added this line instead of the default given below
//$posts[] = get_post($product_id->id);
}
}
return $new_post;
die();
}
return $posts;
}
function get_post_by_tag($tags, $ignoreIds)
{
//Check for
global $wpdb, $wp_query;
$ignoreIdsForMySql = implode(",", $ignoreIds);
//var_dump($ignoreIdsForMySql);
//dynamic query
$query = "select distinct tr.object_id from $wpdb->terms t
inner join $wpdb->term_taxonomy tt on t.term_id = tt.term_id
inner join $wpdb->term_relationships tr on tt.term_taxonomy_id = tr.term_taxonomy_id
inner join $wpdb->posts p on p.ID = tr.object_id
inner join $wpdb->postmeta pm on p.ID = pm.post_id
and t.name IN ('".$tags['destination']."','".$tags['days']."')
where pm.meta_key = '_price' and pm.meta_value = '".$tags['budget']."'
and p.post_status = 'publish'
and p.post_type = 'product'
and (p.post_parent = 0 or p.post_parent is null);";
//var_dump($query);
//Search for the sku of a variation and return the parent.
$matchedProducts = $wpdb->get_results($query) ;
//var_dump($matchedProducts);
if(is_array($matchedProducts) && !empty($matchedProducts))
{
//var_dump($matchedProducts);
$wp_query->found_posts += sizeof($matchedProducts);
//var_dump($wp_query->found_posts, sizeof($matchedProducts));
return $matchedProducts;
}
//return get_post($product_id);
return null;
}
//function get_post_by_filter_tag($price_range,$holiday_type,$tour_category,$ignoreIds) {
function get_post_by_filter_tag($price,$holiday,$tour,$ignoreIds)
{
//Check for
global $wpdb, $wp_query;
//$ignoreIdsForMySql = implode(",", $ignoreIds);
/*$price = $price_range;
$holiday = implode("','", $holiday_type);
$tour = implode("','", $tour_category);*/
//var_dump($ignoreIdsForMySql);
//dynamic query
$query = "select distinct p.ID from $wpdb->terms t
inner join $wpdb->term_taxonomy tt on t.term_id = tt.term_id
inner join $wpdb->term_relationships tr on tt.term_taxonomy_id = tr.term_taxonomy_id
inner join $wpdb->posts p on p.ID = tr.object_id
inner join $wpdb->postmeta visibility on p.ID = visibility.post_id
and visibility.meta_key = '_visibility'
and (visibility.meta_value = 'visible' or visibility.meta_value ='search')
inner join $wpdb->postmeta pm on p.ID = pm.post_id
and t.name IN ('".$holiday."','".$tour."')
where pm.meta_key = '_price' and pm.meta_value <= $price
and p.post_status = 'publish'
and p.post_type = 'product'
and (p.post_parent = 0 or p.post_parent is null);";
//Search for the sku of a variation and return the parent.
$matchedProducts = $wpdb->get_results($query) ;
var_dump($matchedProducts);//die();
if(is_array($matchedProducts) && !empty($matchedProducts))
{
//var_dump($matchedProducts);
$wp_query->found_posts += sizeof($matchedProducts);
//var_dump($wp_query->found_posts, sizeof($matchedProducts));
//var_dump($wp_query);
return $matchedProducts;
}
//return get_post($product_id);
return null;
}