0

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>
````````````````

1 Answer 1

0

You can use the WP Query option "post__not_in" for that, this variable should be an array with IDs of posts that should not appear on the result, so you would use it like that:

$the_query = new WP_Query(array(
        'post__not_in' => array( get_the_ID() ),
        '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',
            )
        ),
    ));

Note that, the get_the_ID() will retrieve the ID of the most recent post from a loop, so you may need to retrieve the ID of the post early and use it instead of the get_the_ID()

2
  • Thank you, this solved the problem! Oct 19, 2022 at 12:24
  • Be aware that post__not_in can have serious performance problems. You'd be better off just querying 2 posts so that you can skip the first result if it happens to be the current post. Oct 19, 2022 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.