0
votes

I need to build an 'announcement' banner showing the 5 latest posts, of which one MUST be the latest newsletter.

What I want to do: latest 4 posts excluding newsletter category + latest newsletter post as the 5th post.

How is this possible?

I have tried tax_query, but then it only shows the newsletter category, and does not limit that specific category.

I have tried excluding all post IDs from the newsletter category, but then if there are no newsletters in the latest 5 posts, it does not show the latest newsletter at all.

Code:

$category_id = get_cat_ID('Newsletter');
$latest_newsletter = query_posts("cat=$category_id&order_by=date");
$exclude_ids = [];
$count = 0;
while (have_posts()) : the_post();
    $post = get_post();
    if($count > 0) {
        $exclude_ids[] = $post->ID;
    }
    $count++;
endwhile;
wp_reset_query();
query_posts(['posts_per_page' => '4', 'order_by' => 'date', 'post__not_in' => $exclude_ids]); if (have_posts()) : ?>
<div class="news-announcement">
    <h3>ANNOUNCEMENT</h3>
    <div id="ticker" class="ticker">
        <ul>
            <?php while ( have_posts() ) : the_post(); ?>
                <li><a href="<?php the_permalink(); ?>" title="Continue reading"> continue reading...</a>
                    <?php the_title(  ); ?></li>
            <?php endwhile; ?>
        </ul>
    </div>
</div>

Any ideas?

1

1 Answers

0
votes

You can create two separate query objects to achieve what you want. e.g

 //this is just an example not complete code
// write you actual query in the array passed to WP_Query
 $latest_articles = new WP_Query( array('order' => 'DESC', 'posts_per_page ' => 4) );

while ( $latest_articles->have_posts() ) : 
    // Loop over the posts and show the content

 $newletter_article = new WP_Query( array('order' => 'DESC', 'posts_per_page'=> 1, 'cat' => 1 ) );

while ( $newletter_article->have_posts() ) : 
 //show the newletter article