Support » Fixing WordPress » Using offset and pagination on custom post type query

  • Hello,
    I have a non-profit site I’m working on, and stuck on getting pagination working for a query. It’s for a featured artist of the month page. The most recent featured artist is pulled out and highlighted at the top (no problem).

    There is then a listing of the 3 most recent past featured artists with pagination below that. That query is also no problem EXCEPT, I need to skip the current featured artist which is already displayed above, and I’m finding that offset can’t be used with custom post type queries with pagination.

    I’ve tested what I found in the Codex here, but no effect:

    Here is the query for the previous featured artists:

    <?php // Load past Featured Artists
    global $paged, $wp_query, $wp;
    if  ( empty($paged) ) {
    	if ( !empty( $_GET['paged'] ) ) {
    	$paged = $_GET['paged'];
    	} elseif ( !empty($wp->matched_query) && $args = wp_parse_args($wp->matched_query) ) {
    	if ( !empty( $args['paged'] ) ) {
    	$paged = $args['paged'];
    	if ( !empty($paged) )
            $wp_query->set('paged', $paged);
    	$temp = $wp_query;
    	$wp_query= null;
    	$wp_query = new WP_Query();
    <ul class="small-block-grid-1 medium-block-grid-3 large-block-grid-3 prev-artists">
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <li class="block">
     // Content from those posts...
    <?php endwhile; ?>
    <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
    <?php $wp_query = null; $wp_query = $temp;?>

    Adding the following (adapted from the Codex post) had no effect – I may not be implementing it correctly…

    /*	Custom Offset for Featured Artists WP_Query
    add_action('pre_get_posts', 'd38_query_offset', 1 );
    function d38_query_offset(&$query) {
        //Before anything else, make sure this is the right query...
        if ( ! $query->is_page('artist-of-the-month') ) {
        //First, define your desired offset...
        $offset = 1;
        //Next, determine how many posts per page you want (we'll use WordPress's settings)
        $ppp = get_option('posts_per_page');
        //Next, detect and handle pagination...
        if ( $query->is_paged ) {
            //Manually determine page query offset (offset + current page (minus one) x posts per page)
            $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
            //Apply adjust page offset
            $query->set('offset', $page_offset );
        else {
            //This is the first page. Just use the offset...
    add_filter('found_posts', 'd38_adjust_offset_pagination', 1, 2 );
    function d38_adjust_offset_pagination($found_posts, $query) {
        //Define our offset again...
        $offset = 1;
        //Ensure we're modifying the right query object...
        if ( $query->is_page('artist-of-the-month') ) {
            //Reduce WordPress's found_posts count by the offset...
            return $found_posts - $offset;
        return $found_posts;

    Any help, much appreciated.


  • The topic ‘Using offset and pagination on custom post type query’ is closed to new replies.