ai didi

Wordpress wp_query group by custom field

转载 作者:行者123 更新时间:2023-12-04 04:34:44 24 4
gpt4 key购买 nike

我必须制作一个搜索表单,我必须在其中填充自定义帖子类型状态的选择。其中一些具有相同的状态,然后我想分组以获得一个干净的状态选择列表..我已经这样做了:

<?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>

结果查询不起作用..请帮助:)
'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 进行一些改进,以使其正常工作:

<?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 group by custom field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19949826/

24 4 0
文章推荐: kendo-ui - 在行中打开详细信息单击 - 剑道网格
文章推荐: php - 日历导出 (Webcal)
文章推荐: php - 图标未显示
文章推荐: Magento Api 错误 fatal error : Uncaught SoapFault exception: [4] Resource path is not callable
行者123
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com