admin管理员组文章数量:1279148
How to combine multiple foreach in only 1 foreach and than sort the foreach based on date?
I am having a WP query based on a custom post type I created called mm_agenda. I know I can create 2 arrays (in both foreach) and than array_merge them, but in that case I only merge the dates and foreach at the end (just) the dates. And my other information/data is gone. How to approach this?
I made the example code a bit shorter, just to be clear. Example code:
<? $agenda_query = new WP_Query(array(
'post_type' => 'mm_agenda',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'meta_key' => 'agenda_startdate',
'order' => 'ASC',
));
if ($agenda_query->have_posts()):?>
<? while ($agenda_query->have_posts()) : $agenda_query->the_post();?>
<? foreach($events_foreach_1 as $event_foreach_1):?>
<? $agenda_startdate = get_post_meta( $post->ID, 'agenda_startdate', true );?>
<div class="date"><?= $agenda_startdate;?></div>
<div class="title"><? the_title();?></div>
<? // And much more data to display, based on the event ?>
<? endforeach;?>
<? foreach($events_foreach_2 as $event_foreach_2):?>
<? $agenda_startdate = get_post_meta( $post->ID, 'agenda_startdate', true );?>
<div class="date"><?= $agenda_startdate;?></div>
<div class="title"><? the_title();?></div>
<? // And much more data to display, based on the event ?>
<? endforeach;?>
<? endwhile;?>
<? endif;?>
Foreach 1 outputs the following: 2021-10-07 Name of event 1
2021-11-08 Name of event 2
Foreach 2 output the following: 2021-09-29 Name of event 3
2021-11-30 Name of event 4
How to collect all that information in 1 foreach or array.. And order it by the startdate. So that there will be 1 list, nicely sorted by startdate. So:
2021-09-29 Name of event 3
2021-10-07 Name of event 1
2021-11-08 Name of event 2
2021-11-30 Name of event 4
Thanks a lot in advance.
本文标签: wp queryHow to merge multiple foreach into 1 foreach and sort by date
版权声明:本文标题:wp query - How to merge multiple foreach into 1 foreach and sort by date 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741297960a2370923.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论