我对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.\' \';
} ?>
SO网友:iyrin
根据询问者的说法,这就是解决方案。他使用了get()
的方法WP_Query
类检索值(颜色)的数组,每个值都用元键存储color
. 因为[color] => \'Yellow\'
可能多次出现在数组中,然后他通过PHP函数过滤数组,array_unique()
只会返回[color] => \'Yellow\'
并忽略值为的任何后续键Yellow
. 将为每个值返回第一次相遇,并且您有一个没有重复值的新数组。
或者,如果您只是处理数组中的字符串值,您可以考虑array_flip( array_flip ( $colors ) )
which is faster 比array_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);