admin管理员组文章数量:1426292
I need to use group by in my posts with a custom field "eventMonth" that will have values like "April-2019" - "June-2019"..
(I never used the wp_query with a meta_query data so...). I've the next query and code in my category fieldfunction
query_group_by_filter($groupby){
global $wpdb;
return $wpdb->postmeta . ".meta_value='eventMonth'";
}:
$query = array(
"category_name"=>"events",
"posts_per_page"=>-1,
"order"=>"ASC",
"meta_query"=>array(
'relation'=>"OR",
array(
'key'=>"eventMonth",
'compare'=> "="
)
),
'meta_key'=>"eventMonth"
);
add_filter( 'posts_groupby', 'query_group_by_filter' );
If I make the loop, I can see only 1 result when I've four posts with this custom field.
How could I do that group by?
Thanks for the help!
I need to use group by in my posts with a custom field "eventMonth" that will have values like "April-2019" - "June-2019"..
(I never used the wp_query with a meta_query data so...). I've the next query and code in my category fieldfunction
query_group_by_filter($groupby){
global $wpdb;
return $wpdb->postmeta . ".meta_value='eventMonth'";
}:
$query = array(
"category_name"=>"events",
"posts_per_page"=>-1,
"order"=>"ASC",
"meta_query"=>array(
'relation'=>"OR",
array(
'key'=>"eventMonth",
'compare'=> "="
)
),
'meta_key'=>"eventMonth"
);
add_filter( 'posts_groupby', 'query_group_by_filter' );
If I make the loop, I can see only 1 result when I've four posts with this custom field.
How could I do that group by?
Thanks for the help!
Share Improve this question asked May 13, 2019 at 7:35 MarcosMarcos 1872 silver badges12 bronze badges 2 |1 Answer
Reset to default 1(Revised answer)
So after discussing via the comments, what you really need is a custom ORDER BY
clause, which in WordPress post queries (WP_Query
), you can set the clause via the posts_orderby
filter just like you can set custom GROUP BY
clause via the posts_groupby
filter.
The order that I need is June - 2019 | May - 2019 | April - 2019 | April - 2019
If the value of the meta eventMonth
is always in this format: {month name} - {4-digit year}
such as June - 2019
— note the dash/hypen which shouldn't be "–" or any other special characters, then the following ORDER BY
clause can sort the posts in the order specified above: (the WordPress table prefix is assumed as being wp_
and this example sorts in descending order)
ORDER BY STR_TO_DATE( REPLACE( wp_postmeta.meta_value, '-', '1,' ), '%M %e, %Y' ) DESC
And the PHP code:
Modify the
ORDER BY
clause:add_filter( 'posts_orderby', 'query_order_by_filter', 10, 2 ); function query_order_by_filter( $orderby, $query ) { // Modify only if and when applicable. if ( 'eventMonth_date' === $query->get( 'orderby' ) ) { global $wpdb; $order = $query->get( 'order' ); return "STR_TO_DATE( REPLACE( $wpdb->postmeta.meta_value, '-', '1,' ), '%M %e, %Y' ) $order"; } return $orderby; }
When making your post/
WP_Query
queries, set theorderby
parameter toeventMonth_date
:$args = array( 'meta_key' => 'eventMonth', 'orderby' => 'eventMonth_date', 'order' => 'DESC', // ... other args. ); $query = new WP_Query( $args );
PS: If you use
get_posts()
instead ofnew WP_Query()
, make sure to setsuppress_filters
tofalse
.
本文标签: wp queryWPQUERY with group by with custom fields
版权声明:本文标题:wp query - WP_QUERY with group by with custom fields 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745474825a2659914.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
eventMonth
for those 4 posts? – Krzysiek Dróżdż Commented May 13, 2019 at 7:59