Blog layout excluding sticky post(s)

Hello,

I’m currently working on blog layout (archive layout) which uses the Wordpress query to get the posts.
As you can see on the screenshot, I have a section which shows the sticky post (we’re going to have just one), a little column to show the top 5 most read (a little plugin I made which excludes the sticky post) and below have all other posts… but, this section also includes the sticky post.

I use the same layout to show the content of the categories by showing/hiding content by using conditions based on what’s currently viewed.

Is it possible to hide / not load the sticky post (“stick post 2” is the same as above) at the section with all other articles (without some sort of first-child() display:none; styling which probably breaks the proper function of the pagination)?

Or should I use my own looper provider to show all other posts? From my experience it’s not a good idea to do so at an archive layout.

best regards
Mirco

Hi Mirco,

Thanks for reaching out.

It is not possible to exclude the Sticky Posts from the default Query, but you can create your looper where it can be excluded.

Hope it helps.
Thanks

thanks @tristup for the quick reply.

But can I use my own looper at the archive layout and the pagination will still work?

I tried that and the pagination shows the correct amount of pages needed to show the other posts, but when using the pagination, it will go to /page/2/ but still showing the same posts as at the first page.

best regards
Mirco

Hi Mirco,

Unfortunately, the pagination will not work in that case.

Thanks

Hi @tristup,

it seems I found some sort of workaround or maybe it’s how it should be build.

What I did:

  1. for the hero post container (sticky one) I’ve set a looper provider and used the query builder and set it to include sticky posts and turned on the looper consumer for the same container to receive just one item and set the consumer to rewind “on”.

  2. for the other posts I’ve just turned on the looper consumer, items to “all” and rewind “off.” This container still contains the sticky post, but at the position based on it’s publishing date. With that I can live :slight_smile:

  3. I’ve added a custom function to the function.php of the child theme:

     function exclude_sticky_posts($query) {
         if ( $query->is_main_query() && !is_admin() && $query->is_home() ) {
             $query->set('ignore_sticky_posts', true);
         }
     }
     add_action('pre_get_posts', 'exclude_sticky_posts');
    

The pagination works as intended and I also don’t have to change the number of posts loaded at the Wordpress settings for “reading” to like 7 to have 6 posts below the hero but 7 on /page/2/, /3/ etc.

This may help other users here if they encounter the same challenge :slight_smile:

best regards
Mirco

Hi Mirco,

This can be a solution, but please remember that it will affect the WordPress default query.

Thanks

Hi @tristup

I know, but this only affects the “is_home()” and what I checked so far all other archive layouts work as intended. Still I will keep an eye on it :slight_smile:

best regards
Mirco

Hello Mirco,

Thank you for your attention to detail and for being proactive in monitoring. Your feedback is valuable to us in ensuring the best experience for our users.

Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.