I have a the following
Custom post type:
episode
Custom taxonomy:
series
- sub terms
real deal
->season 1
,season 2
,season 3
, etc.
- sub terms
The terms are hierarchical, and the entire thing is intended to be structured like a TV series (each "series" has "seasons").
On the single-episode.php
page, I want to display the current episode, AS WELL AS links to the next 3 episodes and previous 3 episodes in that season.
As an example, if on the "Season 1 - Episode 5" page, I need to get content for Season 1,
Episodes 4,3,2 + Season 1 Episodes 6,7,8.
I'm having trouble figuring out how to do this inside the loop on a single episode page.
Here's what I currently have, however it does not work -- it just repeats the current page episode title over and over with a
trying to get property of non-object
error.
<?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:
Here's my updated query, still a work in progress. It does not seem to be getting anything at the moment. I want to get a total of 6 results, 3 posted BEFORE to the current post, 3 posted AFTER the current post. I'm trying to use the post_date
for both the before
and after
properties of date_query
but not sure if I'm doing it right.
$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:
I've gotten my desired effect working using a very inefficient method -- it works, but I'd LOVE to hear some tips to optimize it! seems very expensive in terms of queries right now.
$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;
}
tax_query
portion. Also Not sure how to get posts both before and last without doing two queries.