wordpressloopspostphpquery

post per page is not working with sticky post query in wordpress


this is my post loop that i am using


                  <?php
                        $sticky = get_option( 'sticky_posts' );
                            rsort( $sticky );
                                $args = array(
                                'post_type'           => 'post',
                                'post__in'            => $sticky,
                                'posts_per_page'      => 1
                                );
                           $sticky_query = new WP_Query( $args );
                          while ( $sticky_query->have_posts() ) : $sticky_query->the_post();
                      ?>
                    <article class="cust-arc-post">
                      <img src="<?php the_post_thumbnail_url(); ?>" alt="">
                      <div class="arc-post-header">
                        <a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
                        <a class="cat" href="javascript:;">Category Title</a>
                      </div>
                      <p><?php echo wp_trim_words( get_the_content(), 20, '...' ); ?></p>
                    </article>
                      <?php endwhile;
                        wp_reset_postdata();
                      ?>

i tried using offset but no luck,

i think something is wrong with my loop

if anyone can help me with this thanks in advance


Solution

  • All the information you need can be found in the theme handbook


    As stated in the codex, this displays just the first sticky post, if none return the last post published:

    $args = array(
            'posts_per_page' => 1,
            'post__in' => get_option( 'sticky_posts' ),
            'ignore_sticky_posts' => 1
    );
    $query = new WP_Query( $args );
    

    Your problem likely lies in the rsort(); function, because it's reversing the array from highest to lowest.