CHECKING STATUS
I AM LISTENING TO
|

WordPress + Query_Posts + Sticky + Pagination

5. February 2017
.SHARE

Table of Contents

We often have some posts that we would like to promote and keep at the first page of the blog. When you are using pagination, the sticky posts will be added to the standard posts, making the post count per page uneven.

If you want to keep your posts per page count consistent, there is a way to do that.

PREPARE PAGINATION

global $paged;

if (!isset($paged) || !$paged){
 $paged = 1;
}

// Set posts per page
$posts_per_page  = 8;

// Get sticky posts
$sticky = get_option( 'sticky_posts' );

SWITCH QUERY FOR FIRST PAGE

// First page and has stickies
if($paged == 1 && !empty($sticky)) {

  $args = array(
  'post_type' => 'post',
  'paged' => $paged,
  // Adjust posts per page by subtracting the amount of stickies
  'posts_per_page' => $posts_per_page - count($sticky)
  
  );

}else{
  
  $args = array(
  'post_type' => 'post',
  'paged'		=> $paged,
  'posts_per_page' => $posts_per_page,
  // making sure the 2nd page starts with the right post
   'offset' => (($paged-1)*$posts_per_page) - count($sticky)
  );

}
// Do query
query_posts($args);

Enjoy coding …

Let’s Talk!

Looking for a reliable partner to bring your project to the next level? Whether it’s development, design, security, or ongoing support—I’d love to chat and see how I can help.

Get in touch,
and let’s create something amazing together!

RELATED POSTS

If you have ever embedded an audio player on a podcast site, a music portfolio, or a media archive, you have probably noticed that pretty rendered waveform behind the playhead. Libraries like Wavesurfer.js and Peaks.js can draw those visuals on the client, but decoding a 60-minute MP3 in the browser is slow, memory-hungry, and unreliable […]

Here’s a question that’s been bouncing around dev Slacks and SEO Twitter for the past year: should you bother serving Markdown to AI agents and crawlers? Is it actually worth the effort, or is it just another shiny standard that’ll quietly die like so many before it? Short answer? Yeah, it’s worth doing. The longer […]

Managing multiple social media accounts is exhausting. Between juggling different platforms, scheduling posts at optimal times, and keeping up with analytics, it’s easy to feel overwhelmed. Commercial tools like Buffer and Hootsuite work well but come with hefty subscription fees and feature limitations. Enter Postiz – an open-source, self-hosted social media scheduling platform that puts […]

Alexander

I am a full-stack developer. My expertise include:

  • Server, Network and Hosting Environments
  • Data Modeling / Import / Export
  • Business Logic
  • API Layer / Action layer / MVC
  • User Interfaces
  • User Experience
  • Understand what the customer and the business needs


I have a deep passion for programming, design, and server architecture—each of these fuels my creativity, and I wouldn’t feel complete without them.

With a broad range of interests, I’m always exploring new technologies and expanding my knowledge wherever needed. The tech world evolves rapidly, and I love staying ahead by embracing the latest innovations.

Beyond technology, I value peace and surround myself with like-minded individuals.

I firmly believe in the principle: Help others, and help will find its way back to you when you need it.