I am working on an online magazine in WordPress PHP. I created a custom post type (article) with 4 available custom categories. In the sidebar I want to show the most recent post of all the 4 categories. But if one of the most recent articles are actually open in the browser, I want the sidebar to show the second most recent article in the given category to avoid recurrence. I got to the point where I extracted the slug from the browser url and the slug of the latest post from the given category, but I stopped there. I think I should use an if statement, but I don't quite know how. I appreciate all help, thank you.
sidebar-right.php (this is just one query of the 4 in the sidebar, I repeat this code for all 4 categories):
<?php
global $post;
$post_slug_open = $post->post_name;
?>
<div class="sidebar-preview-container">
<?php
// Define our WP Query Parameters
$the_query = new WP_Query(array(
'post_type' => 'articles',
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'article_category',
'field' => 'slug',
'terms' => 'interju',
)
),
));
?>
<?php
// Start our WP Query
while ($the_query->have_posts()) : $the_query->the_post();
// Display the Post Title with Hyperlink
$article_categories = get_the_terms(get_the_ID(), 'article_category');
$article_title = get_field("article_title");
$article_subtitle = get_field('article_subtitle');
$article_preview_image = get_field('article_preview_image');
$article_slug = get_post_field('post_name', get_post());
setup_postdata($post);
?>
<div class="sidebar-article-preview">
<?php
if ($article_categories) :
foreach ($article_categories as $article_category) :
?>
<a href="<?php echo get_stylesheet_directory_uri(); ?>/<?php echo $article_category->slug; ?>">
<h6 class="article-category-preview">
<?php echo $article_category->name; ?>
<?php echo $article_slug; ?>
</h6>
</a>
<?php
endforeach;
endif;
?>
<a href="<?php the_permalink() ?>">
<h4 class="article-title-preview">
<?php the_title(); ?>
</h4>
</a>
<p class="article-subtitle-preview">
<?php echo $article_subtitle; ?>
</p>
<a href="<?php echo get_permalink(); ?>">
<img class="article-preview-img" src="<?php echo $article_preview_image['url']; ?>" alt="<?php echo $article_preview_image['alt']; ?>">
</a>
</div>
````````````````