Forums

[resolved] Returning only sticky posts (4 posts)

  1. nimaha
    Member
    Posted 1 year ago #

    My first loop returns three sticky posts.

    The second loop returns the most recent posts in every other category. This loop is fine.

    The problem is that in the first loop it returns the sticky posts plus the most recent posts based on what I have set. For example, if I have it as posts_per_page=1, I get the three sticky posts and then whatever the most recent post is. Basically, I need to eliminate posts_per_page but I don't know how to do that and keep just the sticky posts.

    Here is what the first loop looks like.

    <?php $my_query = new WP_Query('posts_per_page=1') ; ?>
    	<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

    My PHP is generally as good as whatever it is I'm reading so I've struggled to find a solution.

  2. vtxyzzy
    Member
    Posted 1 year ago #

    Give this a try:

    <?php
    $args = array(
       'post__in' => get_option('sticky_posts'),
       'caller_get_posts' => 1
    );
    $my_query = new WP_Query($args) ; ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
  3. nimaha
    Member
    Posted 1 year ago #

    Thank you! That did the trick. I was trying to use sticky_posts but could never figure out how it worked with the query I was doing.

  4. vtxyzzy
    Member
    Posted 1 year ago #

    You are welcome!

Topic Closed

This topic has been closed to new replies.

About this Topic