Group posts by meta_key

时间:2013-07-10 作者:mistertodd

我对meta\\u值分组有问题。查询将查找元键为“company”的帖子。我想要一个独特的颜色列表,如:蓝、红、黄

array\\u unique未成功,并且自定义mysql查询也未成功。

<?php
$args = array(
       \'category_name\' => $cat_name,
       \'posts_per_page\' => \'60\',
       \'paged\' => $current_page,
       \'meta_query\' => array(
           array(
               \'key\' => \'company\',
               \'value\' => \'microsoft\',
               \'compare\' => \'like\'
           )
        )
     ); 
$my_query = new WP_Query($args);
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
?>

<?php echo get(\'color\'); ?> 
// Outputs yellow yellow blue yellow red yellow

<?php endwhile; ?>
电流输出为:黄-黄-蓝-黄-红-黄

编辑:

这是最终工作代码:

<?php 
$current_page = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; 
$cat_name = get_category(get_query_var(\'cat\'))->name;


$args = array(
   \'category_name\' => $cat_name,
   \'posts_per_page\' => \'60\',
   \'paged\' => $current_page,
   \'meta_query\' => array(
       array(
           \'key\' => \'company\',
           \'value\' => \'microsoft\',
           \'compare\' => \'like\'
       )
    )
 ); 
$my_query = new WP_Query($args);
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
$colors[] = get(\'color\');

// Creates an array of all colors

endwhile;
$colors = array_unique($colors);
// Removes duplicates;
foreach($colors as $color){
echo $color.\' \';
} ?>

2 个回复
SO网友:iyrin

根据询问者的说法,这就是解决方案。他使用了get() 的方法WP_Query 类检索值(颜色)的数组,每个值都用元键存储color. 因为[color] => \'Yellow\' 可能多次出现在数组中,然后他通过PHP函数过滤数组,array_unique() 只会返回[color] => \'Yellow\' 并忽略值为的任何后续键Yellow. 将为每个值返回第一次相遇,并且您有一个没有重复值的新数组。

或者,如果您只是处理数组中的字符串值,您可以考虑array_flip( array_flip ( $colors ) ) which is fasterarray_unique().

<?php 
$current_page = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; 
$cat_name = get_category(get_query_var(\'cat\'))->name;


$args = array(
   \'category_name\' => $cat_name,
   \'posts_per_page\' => \'60\',
   \'paged\' => $current_page,
   \'meta_query\' => array(
       array(
           \'key\' => \'company\',
           \'value\' => \'microsoft\',
           \'compare\' => \'like\'
       )
    )
 ); 
$my_query = new WP_Query($args);
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;

// Retrieve and store an array of values for the meta_key \'color\'.
// This array may contain duplicate values.
$colors[] = get(\'color\');

endwhile;

// Filter the array to keep only the first key encountered of each unique value.
$colors = array_unique($colors);
// Alternatively, you could replace the above line with a double flip like so:
// $colors = array_flip( array_flip ( $colors ) );

// The array contains no duplicate values at this point.
foreach($colors as $color){
echo $color.\' \';
} ?>

SO网友:Johannes Pille

查看codex页面WP_Query 类,特别是在section titled "Orderby Paramters".

您必须按如下方式调整参数:

$args = array(
    \'category_name\' => $cat_name,
    \'posts_per_page\' => \'60\',
    \'paged\' => $current_page,
    \'meta_key\' => \'color\', /* this has to be included to be able to order by it */
    \'orderby\' => \'meta_value\', /* order by the value of the above key */
    \'order\' => \'ASC\', /* sort direction */
    \'meta_query\' => array(
        array(
            \'key\' => \'company\',
            \'value\' => \'microsoft\',
            \'compare\' => \'like\'
        )
    )
);
$my_query = new WP_Query($args);

结束

相关推荐

如何在MySQL查询上为已有的自定义字段添加前缀?

我有一个问题,在插件更新后,我可以custom fields 在我们的目录网站上可搜索,但仅限于custom fields 获取前缀booking_ 对他们来说。现在有一些页面(已经有30个),每个页面都有几十个自定义字段。我想创建一个自定义字段,称为testfield 成为booking_testfield...因此,所有自定义字段都应具有前缀,但不应中断站点。我正在使用的主题框架将在进入页面时创建新的自定义字段,但那些带有前缀的新字段可以通过主题框架进行管理。