通过自定义字段Wordpress wp_query组 [英] Wordpress wp_query group by custom field
本文介绍了通过自定义字段Wordpress wp_query组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须创建一个搜索表单,我必须通过自定义帖子类型的状态填充选择。其中一些具有相同的状态,然后我想分组以获得干净的状态选择列表。我已经这样做了:
I have to make a search form where i have to populate a select by the states of custom posts types. Some of them have the same state, then I would like to group-by to have a clean select list of states.. I've done this:
<?php
function query_group_by_filter($groupby){
global $wpdb;
return $wpdb->postmeta.".meta_key = 'state'";
}
?>
<?php add_filter('posts_groupby', 'query_group_by_filter'); ?>
<?php $states = new WP_Query(array('post_type' => 'observatoire')); ?>
<?php remove_filter('posts_groupby', 'query_group_by_filter'); ?>
<select id="" class="" name="siege_pays" >
<option value=""></option>
<?php
while ( $states->have_posts() ) : $states->the_post();
echo "<option value=".get_field_object('state').">".get_field_object('state')."</option>";
endwhile;
?>
</select>
结果查询不起作用..请帮助:
the result query doesn't work.. Help please :)
'SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'observatoire' AND ((wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') OR wp_posts.post_status = 'private') GROUP BY wp_postmeta.meta_key = 'siege_pays' ORDER BY wp_posts.post_date DESC LIMIT 0, 10'
推荐答案
我必须对Chirag Patels Code进行一些改进才能使其工作:
I had to make a few improvements to Chirag Patels Code, to get it working:
<?php
function query_group_by_filter($groupby){
global $wpdb;
return $wpdb->postmeta . '.meta_value ';
}
?>
<?php add_filter('posts_groupby', 'query_group_by_filter'); ?>
<?php $states = new WP_Query(array(
'post_type' => 'observatoire',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'state'
));
?>
<?php remove_filter('posts_groupby', 'query_group_by_filter'); ?>
<select id="" class="" name="siege_pays" >
<option value=""></option>
<?php
while ( $states->have_posts() ) : $states->the_post();
echo "<option value=".reset(get_post_custom_values('state')).">".reset(get_post_custom_values('state'))."</option>";
endwhile;
?>
</select>
这篇关于通过自定义字段Wordpress wp_query组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文