Show all sticky posts, WITH pagination

Trying to get a custom admin link working for a custom post type. I’ve managed to add a link to show all “sticky” posts next to the usual “All”, “Published”, “Trash”, etc., but the actual query isn’t working as I’d like it to.

In pre_get_posts, I’m trying to modify the query to do the following:
– show only sticky posts
– respect posts_per_page and pagination settings

Read More

If I try this:

$query->set('post__in',  get_option('sticky_posts'));
$query->set('posts_per_page', 5);

…I see all sticky posts, but no pagination. Does having “posts__in” set mean the posts_per_page is disregarded?

Thanks for the info. Ditched “posts_per_page” but it didn’t make a difference. My debug bar shows:

SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.ID IN (2382,2358,2334,2331,2301,2298,2280,2262,2220,2226,2193,2157,2163,2136,2130,2109,2100,2084,2072,2026,2020,1961,1934,1846,1834,1756,1738,1709,1688,1552,1543,1498,1488,1482,1476,1438,1432,1412,1406,1400,1394,1391,1388,1358,1340,1319,1285,1282,1269,1227,1212,2621,2674,2659,3300,3313,3414,3560,3314) AND wp_posts.post_type = 'ad_listing' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC 

Related posts

Leave a Reply

1 comment

  1. Based on this post, i did this test and go it working:

    $sticky = get_option( 'sticky_posts' );
    $ppp = get_option('posts_per_page');
    
    if (!is_paged()) {
        $custom_offset = 0;
    } else {
        $custom_offset = $ppp*($paged-1);
    }
    
    $args = array(
        'numberposts' => $ppp,
        'offset' => $custom_offset,
        'post__in' => $sticky 
    );
    
    $posts_data = get_posts( $args );
    $pd = count( $posts_data );
    
    if ( count( $posts_data ) > 0 ) {
        echo '<ul>';
        foreach ( $posts_data as $post ) {
            echo '<li><a href="'.get_permalink( $post->ID ).'">'.$post->post_title.'</a></li>';
        }
        echo '</ul>';
    } 
    
    if ( $pd != 1 ) { next_posts_link( __( '<span class="meta-nav">&laquo;</span> P&aacute;gina anterior ', 'twentyten' ) ); }
    previous_posts_link( __( 'Pr&oacute;xima p&aacute;gina <span class="meta-nav">&raquo;</span>', 'twentyten' ) );
    

    I tested it with the reading parameter set to 2 and 4 and it seems ok. See if it suits your needs.