[英]How can I separate the output of a wp_query into separate sections
我有一個WP查詢正在檢查自定義的帖子類型元字段。
我有兩種自定義帖子類型:
只需創建國家/地區自定義帖子類型即可將媒體帖子類型的內容拉入其中。
媒體CPT包含2個元區域:
_rtl_media_filter
。 _rtl_country_filter
,它將國家/地區CPT名稱拉入下拉列表,並且可以從媒體帖子類型中選擇。 目的是在查看國家/地區CPT時顯示與國家/地區CPT相關的所有媒體類型。
我有這個循環,這是有效的。 但我想知道我是否可以按媒體類型_rtl_media_filter
對單個循環項目進行_rtl_media_filter
? 而不是為每個創建多個循環?
<?php
// Grab this posts ID
$post = $wp_query->post;
$this_id = $post->ID;
// Query the meta vaules against the posts ID
$args = array(
'post_type' => 'media',
'meta_query' => array(
'relation' => 'OR',
array( // TV
'relation' => 'AND',
array(
'key' => '_rtl_media_filter',
'value' => 'tv',
'compare' => '=',
),
array(
'key' => '_rtl_country_filter',
'value' => $this_id,
'compare' => '=',
),
),
array( // DIGITAL
'relation' => 'AND',
array(
'key' => '_rtl_media_filter',
'value' => 'digital',
'compare' => '=',
),
array(
'key' => '_rtl_country_filter',
'value' => $this_id,
'compare' => '=',
),
),
array( // RADIO
'relation' => 'AND',
array(
'key' => '_rtl_media_filter',
'value' => 'radio',
'compare' => '=',
),
array(
'key' => '_rtl_country_filter',
'value' => $this_id,
'compare' => '=',
),
),
),
'posts_per_page' => -1,
);
$the_query = new WP_Query( $args );
if($the_query -> have_posts()) :
while ($the_query -> have_posts()) : $the_query -> the_post();
echo '<h2>' . the_title() . '</h2>';
endwhile;
wp_reset_postdata();
endif;
?>
這就是我想要的輸出:
ITV | ITV2 | ITV3
收音機1 | 第二廣播電台
HUB 1 | HUB 2
因此,在Antonis指出按meta_value排序后,我想出了它可以做到的方式。 對於其他任何嘗試這個問題的人來說,解決方案就是這幾行:
'orderby' => 'meta_value',
'meta_key' => '_rtl_media_filter',
將它們添加到循環查詢的底部。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.