0

In the category post archive, I want to display the last 10 posts and 1 featured post (featured post only on the first page). I can choose a featured post that will be displayed first (user can select manually, its custom field is called ‘category_featured_post’ ). Now if the featured post is the same post that will be displayed in a list, It will be doubled (on featured position and on the list). I can hide this post on the list (if it is displayed on the first page where the featured post is displayed), but now there will be 9 posts on the list instead of 10. How can I display 10 posts in that case?

Custom field category_featured_post is on category, it is a Post Object that returns a single post ID.

Here is the code I have now:

<!-- DISPLAY FEATURED POST (ONLY ON FIRST PAGE) IN BIG TILE -->
<?php
    if ( !is_paged() ) :
        $post = get_post($featured_post);
        ?>
        <div class="tile">
            <h3><?php the_title(); ?></h3>
            <p><?php the_excerpt(); ?></p>
            <a href="<?php echo the_permalink(); ?>">Read post</a>
        </div>
        <?php
    endif;     
?>
<!-- DISPLAY POSTS IN LIST -->
<?php 
    if (have_posts()){
        while(have_posts()){
            the_post();

            // DON'T DISPLAY POST IF IT'S FEATURED
            if(get_the_ID() != $featured_post ){
                get_template_part('template-parts/post/content','archive');
            }
        }
    } 
?>

10
  • by the time archive.php is loaded the query has already ran, so changing its parameters will not change the number of posts because it's already fetched the posts, it's too late. Making it work this way would mean discarding the query and putting a new one in ( very bad for performance, will introduce lots of pagination issues ), or it would require time travelling into the past several micro-seconds. Additionally, post archives don't have an ID because they aren't posts/pages, they're archives
    – Tom J Nowell
    Commented Nov 1, 2021 at 19:17
  • Instead of asking how to implement a proposed solution, ask how to solve your problem, what are you actually trying to do that requires this?
    – Tom J Nowell
    Commented Nov 1, 2021 at 19:18
  • Thank for the answer, I edited the question.
    – mikolaj
    Commented Nov 1, 2021 at 20:01
  • you mentioned a custom field, but what is the custom field on? A category? archive.php covers all archives, date archives, tag archives etc etc, can you be more specific?
    – Tom J Nowell
    Commented Nov 1, 2021 at 21:19
  • Custom field category_featured_post is on category, it is a Post Object that returns a single post ID. I'm talking about posts archive.
    – mikolaj
    Commented Nov 1, 2021 at 21:24

1 Answer 1

0

Thanks to liquidRock and StudioAl for solution!

Exclude post from query:

'post__not_in' => array( $featured_post_id )

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.