0

I would like to display 9 posts per page. But, when I have a sticky post, I would like it to be integrated in the 9 posts in the query. And, then, for the second page, I continue to have the next 9 posts... So I did this:

$sticky_count = count(get_option( 'sticky_posts' ));
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts_per_page = 9;
if(!is_paged()):
   $posts_per_page = $posts_per_page - $sticky_count;
endif;
$params = ['post_type' => 'projects', 'order' => 'DESC', 'posts_per_page' => $posts_per_page, 'paged' => $paged];

$my_query = new WP_Query( $params );?>
<?php $c_array = $my_query->get_posts(); ?>
     <?php foreach ($c_array as $project) : ?>
           <div class="cell small-12 medium-6 large-4">
                <?php include locate_template('parts/one_project.php'); ?>
           </div>
    <?php endforeach; ?>

With this code, I have 9 posts on my first page, including the sticky post. I also have 9 posts on the other pages. However, one post is missing!

Let's say I put the 14 in sticky. So on my first page I have: 14, 1, 2, 3 ... 8. On the second page, it's the 10th post that appears. The ninth has disappeared... How to do ?

Thanks a lot for help

EDIT --- I tried to do that but I still have the problem since the request is re-executed on each page...

$sticky = count(get_option('sticky_posts'));
$posts_per_page = 9;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$params = [
    'post_type' => 'projects',
    'order' => 'DESC',
    'posts_per_page' => $posts_per_page,
    'paged' => $paged,
    'offset' => 9 - $sticky
];
if (!is_paged()):
    $posts_per_page = 9 - $sticky;
    $params = [
        'post_type' => 'projects',
        'order' => 'DESC',
        'posts_per_page' => $posts_per_page,
        'paged' => $paged,
    ];
endif;
5
  • include locate_template( should be get_template_part(. Otherwise the query on your second page is the second page of a different query to the one that is on your first page, so naturally the results are different, the post isn't missing, you're just using a new distinct query that has different results. You might also find your sticky post appear a second time as it comes up as a normal post on a later page. You'll need to find some way to adjust the offset used to fix this, or ideally, use the same query parameters for all pages and find a new way to skip sticky posts on the other pages
    – Tom J Nowell
    Commented Mar 16, 2022 at 14:52
  • additionally, is_paged may not work the way you're intending it to, as it refers to the main query, not the custom query. At a minimum it would need to be checked after you create the $my_query variable, otherwise how would it know if the query is paged or not before the query has happened without resorting to time travel?
    – Tom J Nowell
    Commented Mar 16, 2022 at 14:55
  • Thank you, but how can I do to have 9 posts on each page without redundancy or forgetfulness? And ok for the other advices.
    – Gpi
    Commented Mar 16, 2022 at 15:58
  • I don't know how can I do this because when I use ignore_sticky_posts for the other pages for example I have the same problem. I always return to the starting point...
    – Gpi
    Commented Mar 16, 2022 at 16:28
  • "use the same query parameters for all pages and find a new way to skip sticky posts on the other pages" => I would love to find another way to do it !
    – Gpi
    Commented Mar 21, 2022 at 7:59

0

Your Answer

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