Support » Fixing WordPress » Returning only sticky posts

  • Resolved nimaha

    (@nimaha)


    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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(); ?>
    Thread Starter nimaha

    (@nimaha)

    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.

    You are welcome!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Returning only sticky posts’ is closed to new replies.