我有以下几点
自定义帖子类型:episode
自定义分类法:series
子条款real deal
-> season 1
, season 2
, season 3
, 等等。这些术语是分级的,整个内容的结构就像一部电视剧(每个“系列”都有“季节”)。
在single-episode.php
第页,我想显示当前剧集,以及该季下3集和前3集的链接。
例如,如果在“第1季-第5集”页面上,我需要获取第1季的内容,
第4、3、2集+第1季第6、7、8集。
我很难在单集页面的循环中完成这一点。
这是我目前拥有的内容,但它不起作用——它只是用一个
尝试获取非对象的属性
错误
<?php
$args = array(
\'post-type\' => \'episode\',
\'post-status\' => \'publish\',
\'posts_per_page\' => 6,
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'series\',
\'field\' => \'slug\',
/* Name of the "series" (in slug format) */
\'terms\' => array( \'season-1\' ),
),
array(
\'taxonomy\' => \'series\',
\'field\' => \'slug\',
/* Name of the "seasons" (in slug format) DYNAMIC */
\'terms\' => array( $term->slug ),
)
)
);
$episodes = new WP_Query( $args );
foreach( $episodes as $episode ) {
echo get_the_title($episode->id);
}
?>
<小时>
EDIT:
这是我更新的查询,仍在进行中。目前似乎没有任何进展。我想得到总共6个结果,3个在当前帖子之前发布,3个在当前帖子之后发布。我正在尝试使用post_date
对于before
和after
的属性date_query
但不确定我是否做对了。
$args = [
\'tax_query\' => [
\'relation\' => \'AND\', [
\'taxonomy\' => \'series\',
\'field\' => \'slug\',
/* Name of the "series" (in slug format) */
\'terms\' => [\'season-1\'],
]
],
\'posts_per_page\' => 6,
/* make query more efficient */
\'no_found_rows\' => true,
/* dont let filters/pre_get_posts modify query */
\'suppress_filters\' => true,
\'date_query\' => [
[
\'before\' => $post_object->post_date,
\'after\' => $post_object->post_date,
],
\'inclusive\' => false
]
];
$q = new WP_Query( $args );
return $q->posts;
<小时>
EDIT 2:
我用一种效率很低的方法工作,得到了我想要的效果——它是有效的,但我很想听到一些优化它的提示!就目前的查询而言,似乎非常昂贵。
$orig_post = $post;
$orig_terms = wp_get_post_terms($orig_post->ID, \'series\');
$current_post = $post;
$adjPost = [
\'prev\' => [],
\'next\' => []
];
for($i = 1; $i <= 3; $i++){
$post = get_previous_post(true, \'\', \'series\'); // this uses $post->ID
if ( $post ) {
$these_terms = wp_get_post_terms($post->ID, \'series\');
if ( $these_terms[1]->slug === $orig_terms[1]->slug) {
array_push( $adjPost[\'prev\'], $post );
}
}
}
$post = $current_post;
for($i = 1; $i <= 3; $i++){
$post = get_next_post(true, \'\', \'series\'); // this uses $post->ID
if ( $post ) {
$these_terms = wp_get_post_terms($post->ID, \'series\');
if ( $these_terms[1]->slug === $orig_terms[1]->slug) {
array_push( $adjPost[\'next\'], $post );
}
}
}
$post = $current_post;
echo "<h1>Prev:</h1>";
foreach ( $adjPost[\'prev\'] as $prev ) {
echo \'<br>\';
echo $prev->post_title;
}
echo "<h1>Next:</h1>";
foreach ( $adjPost[\'next\'] as $next ) {
echo \'<br>\';
echo $next->post_title;
}
最合适的回答,由SO网友:Pieter Goosen 整理而成
你的第二种方法(EDIT 2) 不幸的是,它有很多车,效率很低。此外,您不会在一个查询中执行此操作。
正如我已经说过的,您需要在this answer 我最近做过。你在第一次编辑时几乎做到了,唯一的问题是,你不能在一个查询中完成这项工作,你必须完成两项工作,一项是获取前一组帖子,另一项是获取下一组帖子。
我优化了代码,只获得了必要的信息,什么都没有。WP_Query
结果也会被缓存,所以您真的不必太担心效率。
我不会在这个答案中再次重复所有细节,你应该详细阅读链接帖子,但你可以尝试类似的内容(警告:未测试。请参阅链接答案中的注释)
$post_object = get_queried_object();
$terms = wp_get_post_terms( $post_object->ID, \'series\', array( \'fields\' => \'ids\' ) ); // Set fields to get only term ID\'s to make this more effient
$args = [
\'post_type\' => $post_object->post_type,
\'tax_query\' => [
[
\'taxonomy\' => \'series\',
\'terms\' => $terms,
]
],
\'posts_per_page\' => 3,
\'order\' => \'ASC\' // CHANDE TO DESC IF NOT CORRECT
/* make query more efficient */
\'no_found_rows\' => true,
/* dont let filters modify query */
\'suppress_filters\' => true,
\'date_query\' => [
[
\'before\' => $post_object->post_date,
\'inclusive\' => false
],
]
];
$q1 = new WP_Query( $args );
var_dump( $q1->posts );
$args1 = [
\'post_type\' => $post_object->post_type,
\'tax_query\' => [
[
\'taxonomy\' => \'series\',
\'terms\' => $terms,
]
],
\'posts_per_page\' => 3,
\'order\' => \'DESC\' // CHANDE TO ASC IF NOT CORRECT
/* make query more efficient */
\'no_found_rows\' => true,
/* dont let filters modify query */
\'suppress_filters\' => true,
\'date_query\' => [
[
\'after\' => $post_object->post_date,
\'inclusive\' => false
],
]
];
$q2 = new WP_Query( $args1 );
var_dump( $q2->posts );