Adding Offset With Limit To wp_get_archives for pagination.

Imran Sayed
2 min readDec 23, 2021

--

In this blog, we will learn about how to add the offset and the limit for pagination. In this example, we are showing this for monthly archives, but you can do it similarly for the daily and yearly as well.

function get_custom_monthly_archives( $args = []) {

global $wpdb, $wp_locale;

$defaults = [
'type' => 'monthly',
'limit' => '',
'offset' => '',
'format' => 'html',
'before' => '',
'after' => '',
'show_post_count' => false,
'echo' => 1,
'order' => 'DESC',
'post_type' => 'post',
'year' => get_query_var( 'year' ),
'monthnum' => get_query_var( 'month' ),
'day' => get_query_var( 'day' ),
'w' => get_query_var( 'w' ),
];

$parsed_args = wp_parse_args( $args, $defaults );

$output = '';
$last_changed = wp_cache_get_last_changed( 'posts' );
$join = apply_filters( 'getarchives_join', '', $parsed_args );
$sql_where = $wpdb->prepare( "WHERE post_type = %s AND post_status = 'publish'", $parsed_args['post_type'] );
$where = apply_filters( 'getarchives_where', $sql_where, $parsed_args );

$order = $parsed_args['order'];

if ( ! empty( $parsed_args['limit'] ) && empty( $parsed_args['offset'] ) ) {
$limit = sprintf( ' LIMIT %1$s', intval( $parsed_args['limit'] ) );
} elseif ( ! empty( $parsed_args['limit'] ) && ! empty( $parsed_args['offset'] ) ) {
$limit = sprintf( ' LIMIT %1$s,%2$s', intval( $parsed_args['offset'] ), intval( $parsed_args['limit'] ) );
} else {
$limit = '';
}

$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit";
$key = md5( $query );
$key = "wp_get_archives:$key:$last_changed";
$results = wp_cache_get( $key, 'posts' );

if ( ! $results ) {
$results = $wpdb->get_results( $query );
wp_cache_set( $key, $results, 'posts' );
}
if ( $results ) {
$after = $parsed_args['after'];
foreach ( (array) $results as $result ) {
$url = get_month_link( $result->year, $result->month );
if ( 'post' !== $parsed_args['post_type'] ) {
$url = add_query_arg( 'post_type', $parsed_args['post_type'], $url );
}
/* translators: 1: Month name, 2: 4-digit year. */
$text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $result->month ), $result->year );
if ( $parsed_args['show_post_count'] ) {
$parsed_args['after'] = ' (' . $result->posts . ')' . $after;
}
$selected = is_archive() && (string) $parsed_args['year'] === $result->year && (string) $parsed_args['monthnum'] === $result->month;
$output .= get_archives_link( $url, $text, $parsed_args['format'], $parsed_args['before'], $parsed_args['after'], $selected );
}
}

return $output;
}

Using this function.

get_custom_monthly_archives(['limit'=> 1, 'offset'=>'2']);

That’s all folks!

--

--

Imran Sayed
Imran Sayed

Written by Imran Sayed

👤 Full Stack Developer at rtCamp, Speaker, Blogger, YouTuber, Wordpress, React, Node, Laravel Developer http://youtube.com/ImranSayedDev

No responses yet