WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to limit query to recent piosts excluding the most recent (4 posts)

  1. skeeterz71
    Member
    Posted 6 months ago #

    Hello,

    I am wanting to display the titles of 3 recent posts which excluding the most recent. I currently use this, but it displays the most recent post. How do I alter this or is there a better function for what I want to accomplish.

    `<?php wp_get_archives( array( 'type' => 'postbypost', 'limit' => 3, 'format' => 'html' ) ); ?>'

    Thanks

  2. keesiemeijer
    moderator
    Posted 6 months ago #

    Try it with get_posts().
    http://codex.wordpress.org/Template_Tags/get_posts

    <?php
    $args = array( 'posts_per_page' => 3, 'offset' => 1 );
    $posts = get_posts( $args );
    
    if ( $posts ) {
    	echo '<ul>';
    	foreach ( $posts as $post ) {
    
    		echo '<li><a href="' . esc_url( get_permalink( $post->ID ) ). '">';
    		echo get_the_title( $post->ID ) . '</a></li>';
    	}
    	echo '</ul>';
    	wp_reset_postdata();
    }
    ?>
  3. skeeterz71
    Member
    Posted 6 months ago #

    Thanks. After going about 4 levels deep on google after 10 or so searches I finally found it. This will pull 3 posts except for the most recent. That is what the offset does. This includes the title and link. That is all I wanted in the display.

    <h3>Recent Posts</h3>
    
      <ul>
      <?php query_posts('posts_per_page=3&offset=1'); ?>
    	<?php while (have_posts()) : the_post(); ?>
    			<li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
    	<?php endwhile; ?>
      </ul>
  4. keesiemeijer
    moderator
    Posted 6 months ago #

    You shouldn't use query_posts() as it's meant for the main query. Did you try my example? It's basically the same as yours without using query_posts.

    http://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts

Reply

You must log in to post.

About this Topic

Tags

No tags yet.