1

My client shows his 5 most recent posts (excerpts) on the homepage followed by a "read more" link. On archive page he does not want to display the 5 most recent post again but posts from #6 to #20.

How can I do this?

2 Answers 2

2

Add offset in the query and give value as 5 so, the first 5 will be skipped.

Below is code snippet for the same

$custom_args = array('post_type' => 'your custom post type name',
'posts_per_page' => '20',
'orderby' => 'id',
'offset'=>5,
'order' => 'ASC',);
$custom_query = get_posts($custom_args);
 foreach ($custom_query as $value) {
 //your data
  }
1
  • This means you run a second query after the original archive query. It's better to filter the original query with pre_get_posts or similar.
    – Hans
    Sep 19, 2018 at 9:38
1

You can filter the original archive query:

function my_archive_query( $query ) {
  if ( $query->is_archive() && $query->is_main_query() ) {
    $query->set( 'offset', 5 );
    $query->set( 'posts_per_page', 20 );
  }
}

add_action( 'pre_get_posts', 'my_archive_query' );

More info:

pre_get_posts filter

is_archive conditional tag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.