转发器字段查询:按子字段值排序
在 WP 网站上,我有一个自定义帖子类型(“客户”),带有中继器(“付款”)和 2 个子字段:日期和金额。
我有一个查询循环遍历所有帖子并将付款放入表中。
Client 1 - 12/01/2022 - 50€
Client 1 - 23/02/2022 - 170€
Client 2 - 02/12/2021 - 1000€
Client 2 - 07/03/2022 - 4000€
Client 3 - 10/02/2022 - 100€
有什么办法可以按付款日期订购这张桌子吗?
这是我的代码:(
<table>
<?php $args = array(
'post_type' => 'clients',
'posts_per_page' => '-1',
);
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
$post_query->the_post();
$payments = get_field('payments');
if( $payments ) {
foreach( $payments as $payment ) {
$date = $payment['date'];
$amount = $payment['amount'];
?>
<tr>
<td><?php echo the_title(); ?></td>
<td><?php echo $date; ?></td>
<td><?php echo $amount; ?></td>
</tr>
<?php }}}} ?>
</table>
上面的代码可能包含错误;我在这篇文章中使用了我的实际代码并对其进行了很多简化,所以我没有测试它)
提前非常感谢您!
On a WP website, I have a custom post type ("Clients") with a repeater ("Payments") and 2 subfields : Date and Amount.
I have a query that loops through all the posts and puts the payments in a table.
Client 1 - 12/01/2022 - 50€
Client 1 - 23/02/2022 - 170€
Client 2 - 02/12/2021 - 1000€
Client 2 - 07/03/2022 - 4000€
Client 3 - 10/02/2022 - 100€
Is there any way to order this table by date of payment ?
Here is my code :
<table>
<?php $args = array(
'post_type' => 'clients',
'posts_per_page' => '-1',
);
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
$post_query->the_post();
$payments = get_field('payments');
if( $payments ) {
foreach( $payments as $payment ) {
$date = $payment['date'];
$amount = $payment['amount'];
?>
<tr>
<td><?php echo the_title(); ?></td>
<td><?php echo $date; ?></td>
<td><?php echo $amount; ?></td>
</tr>
<?php }}}} ?>
</table>
(the code above might contain mistakes ; i took my actual code and simplified it a lot for this post, so i didn't test it)
Thank you very much in advance !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论