这里有一个简单的解决方案。但要求您拥有最新版本的WordPress。(或至少4.1)
使用嵌套分类查询。
拿走你所有的,再加上一点。
$args = array(
\'post-type\' => \'episode\',
\'post-status\' => \'publish\',
\'posts_per_page\' => 4,
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'series\',
\'field\' => \'slug\',
\'terms\' => array( \'real-deal\' ),// Name of the series (in slug format)
),
array(
\'taxonomy\' => \'series\',
\'field\' => \'slug\',
\'terms\' => array( \'season-1\' ),// Name of the season (in slug format)
)
)
);
$episodes = new WP_Query( $args );
print_r( $episodes->posts );
也就是说,找到属于特定系列且包含特定季节的4个最新发布的剧集。(这就是你的视觉模型所描绘的)
你提到需要“季节”是动态的。
以下是如何从父类别(系列)中获取所有“子类别”(季节),然后从该系列以及所有可能的季节中检索最新的4集。
$taxonomy = \'series\';
$seasons = get_terms( $taxonomy, array(
\'parent\' => 1, // TERM ID OF THE SERIES
\'fields\' => \'id=>slug\'
) );
if ( ! empty( $seasons ) && ! is_wp_error( $seasons ) ) {
$args = array(
\'post-type\' => \'episode\',
\'post-status\' => \'publish\',
\'posts_per_page\' => 4,
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => $taxonomy,
\'field\' => \'slug\',
\'terms\' => array( \'industry-news\' ),// Name of the "series" (in slug format)
),
array(
\'taxonomy\' => $taxonomy,
\'field\' => \'slug\',
\'terms\' => array_values( $seasons ),// Name of the "seasons" (in slug format) DYNAMIC
)
)
);
$episodes = new WP_Query( $args );
print_r( $episodes->posts );
}
假设你在一个系列中有三季。。。您希望显示本系列中每个季度的4个最新剧集。
$taxonomy = \'series\';
$seasons = get_terms( $taxonomy, array(
\'parent\' => 1, // TERM ID OF THE SERIES
\'fields\' => \'id=>slug\'
) );
$season_episodes = array();
if ( ! empty( $seasons ) && ! is_wp_error( $seasons ) ) {
foreach ( array_values( $seasons ) as $season ) {
$season_episodes[ $season ] = array();// Placeholder in-case there\'s no episodes found.
$args = array(
\'post-type\' => \'episode\',
\'post-status\' => \'publish\',
\'posts_per_page\' => 4,
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => $taxonomy,
\'field\' => \'slug\',
\'terms\' => array( \'industry-news\' ),// Name of the "series" (in slug format)
),
array(
\'taxonomy\' => $taxonomy,
\'field\' => \'slug\',
\'terms\' => array( $season ),// Name of the "seasons" (in slug format) DYNAMIC
)
)
);
$episodes = new WP_Query( $args );
if ( ! empty( $episodes->posts ) ) {
$season_episodes[ $season ] = $episodes->posts;// Add all episodes found in this season.
}
}
}
if ( ! empty( $season_episodes ) ) {
print_r( $season_episodes );
}
最后一种方法是将一个系列中最新的4集按季节分类,将这4篇帖子放入单独的数组中。
综上所述,在包含4个最近剧集的系列中,每个季度都有一个数组。你所要做的就是输出信息。