如何使用wp_Query按多个定制字段排序?

时间:2011-04-07 作者:Industrial Themes

我有一个分级系统,用户可以输入一个自定义字段“Stars”或“Hearts”,但无论他们选择哪一个,都必须输入1到5的值。使用wp\\u query,我可以成功地按其中一个自定义字段进行排序,但我不确定如何同时按这两个字段进行排序。下面是我用来按其中一个自定义字段“Stars”排序的代码:

$args=array(
\'posts_per_page\' => $con_review_num,
\'post_type\' => \'con_product_reviews\',
\'paged\' => $paged,
\'orderby\' => \'meta_value\',
\'order\' => \'DESC\',
\'meta_key\' => \'Stars\'   
);
下面是我希望此函数可以执行的操作(请注意最后一行):

$args=array(
\'posts_per_page\' => $con_review_num,
\'post_type\' => \'con_product_reviews\',
\'paged\' => $paged,
\'orderby\' => \'meta_value\',
\'order\' => \'DESC\',
\'meta_key\' => \'Stars Hearts\'    
);
还有别的方法吗?

3 个回复
最合适的回答,由SO网友:saalon 整理而成

我也遇到了同样的问题,无法单独使用wp\\U查询来实现这一点。相反,我捕获了posts\\u子句过滤器,并将我的order\\u卡在那里。这也意味着要进行一些连接。

基本上,您可以设置一个函数来捕获posts\\u子句(或一个限制更严格的查询过滤器;但现在我们只使用它),然后在该函数中连接到所需字段的post\\u元表,然后按它们排序。

重要的是设置一些东西,然后删除查询过滤器。您可以在捕获它的函数的末尾执行此操作,但我的插件中有单独的函数,所以我将继续使用它。“wp”事件在这里运行良好。

add_filter(\'posts_clauses\', \'filterMarkets\')
add_filter(\'wp\', \'removeQueryFilter\');

public static function removeQueryFilter() {
  remove_filter(\'posts_clauses\', \'filterMarkets\');
}

public static function filterMarkets($clauses, $wp_query) {
global $wpdb;

$clauses[\'join\'] .=<<<SQL
JOIN {$wpdb->postmeta} AS title_meta ON title_meta.post_id = {$wpdb->posts}.ID AND
title_meta.meta_key = \'fest_sortby\'
JOIN {$wpdb->postmeta} AS start_date_meta ON start_date_meta.post_id = {$wpdb->posts}.ID
AND start_date_meta.meta_key = \'fest_market_start_date\'
SQL;

  $clauses[\'orderby\'] .= " title_meta.meta_value ASC";
  $clauses[\'orderby\'] .= " start_date_meta.meta_value ASC";
}
return $clauses;
}

SO网友:Bainternet

创建两个自定义字段如何:

rating_type = \'stars\' or \'hearts\'
rating_score = 1-5
然后,您可以按一个自定义字段进行订购:

$args=array(
\'posts_per_page\' => $con_review_num,
\'post_type\' => \'con_product_reviews\',
\'paged\' => $paged,
\'orderby\' => \'meta_value\',
\'order\' => \'DESC\',
\'meta_key\' => \'rating_score\'   
);
要知道要显示哪种评级,请使用:

$rating_type = get_post_meta($post->ID,\'rating_type\',true);
if ($rating_type == "stars"){
   //show star rating
}else{
   //show heartsrating
}

SO网友:Industrial Themes

下面是我如何仅使用一个自定义字段来完成这项工作的。我没有使用“Stars”或“Hearts”作为自定义字段名称,“1”、“2”、“3”等作为值,而是使用“Rating”作为名称,“1星”、“2星”、“3星”等(或用Hearts代替Stars)作为值。因为红心和星星的测量单位都是0到5,所以我可以只按评级的一个自定义字段值进行排序,然后解析出显示值。这是我的代码:

<?php 

// setup the query
$args=\'&posts_per_page=\'.$con_review_num.\'&post_type=con_movie_reviews&paged=\'.$paged.\'&order=DESC&orderby=\'.$feedsort.\'&meta_key=Rating\';

$review_loop = new WP_Query($args);

if ($review_loop->have_posts()) : while ($review_loop->have_posts()) : $review_loop->the_post(); $postcount++;

    // get the custom field value
    $rating = get_post_meta($post->ID, "Rating", $single = true);

    $unit = "star"; //default unit is star          
    if(strpos($rating," Hearts") || strpos($rating," hearts") || strpos($rating," Heart") || strpos($rating," heart")) {
    $unit = "heart"; //find out if we\'re using hearts instead of stars
    }
    // extract just the number from the rating meta key
    $rating = trim(str_replace(" star","",str_replace(" stars","",str_replace(" Star","",str_replace(" Stars","",str_replace(" heart","",str_replace(" hearts","",str_replace(" Heart","",str_replace(" Hearts","",$rating)))))))));
    if($stars) { // are we using stars or hearts?
        $rating = $stars;
        $unit = "star";
    } elseif($hearts) {
        $rating = $hearts;
        $unit = "heart";
    }   
    ?>

    <div class="stars tooltip" title="<?php echo $rating; ?> / 5 <?php echo $overall; ?>">

        <div class="<?php echo $unit; ?><?php if($rating>=1) { ?> full<?php } elseif($rating>0) { ?> half<?php } ?>">&nbsp;</div>

        <div class="<?php echo $unit; ?><?php if($rating>=2) { ?> full<?php } elseif($rating>1) { ?> half<?php } ?>">&nbsp;</div>

        <div class="<?php echo $unit; ?><?php if($rating>=3) { ?> full<?php } elseif($rating>2) { ?> half<?php } ?>">&nbsp;</div>

        <div class="<?php echo $unit; ?><?php if($rating>=4) { ?> full<?php } elseif($rating>3) { ?> half<?php } ?>">&nbsp;</div>

        <div class="<?php echo $unit; ?><?php if($rating>=5) { ?> full<?php } elseif($rating>4) { ?> half<?php } ?>">&nbsp;</div>

    </div>

    <?php 

}
endwhile;
endif;                  

?>

结束

相关推荐