• Hello everyone

    I’m completely stuck in having to paginate a custom post loop. The solution given in in this page makes sense to me, but did not work for me:

    https://css-tricks.com/snippets/wordpress/paginate-custom-post-types/

    The below is the code I embedded into the post listing template. It only takes me to the top page when I click a page number or the next link:

    
      $wp_query = new WP_Query( array( 'post_type' => 'office-magazines',
                                     'paged'     =>  $paged ));
              $total = $wp_query->max_num_pages; 
              // For more than 1 page
              if ( $total > 1 )  {
                   // get the current page
                   if ( !$current_page = get_query_var('paged') )
                        $current_page = 1;
                  
                   if( get_option('permalink_structure') ) {
                       $format = '?paged=%#%';
                   }
                   echo paginate_links(array(
                        'base'     => get_pagenum_link(1) . '%_%',
                        'format'   => $format,
                        'current'  => $current_page,
                        'total'    => $total,
                        'mid_size' => 4,
                        'type'     => 'list'
                   ));
              }
              wp_reset_postdata();
    

    where ‘office-magazines’ is the custom post type I registered.

    Other code in a very similar format worked to give me a pagination for the main post loop. Could someone please spot me any errors with the code above?

    Hoping to receive some tips,

    Thank you,

    Ead

    • This topic was modified 8 years, 1 month ago by Jan Dembowski. Reason: Fixing formatting
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    You didn’t mention which pagination functions you use to output the paged links. I’m fairly sure they only work for main post loops. Once you make a custom query, the usual pagination functions don’t work because they still think you are working off the main post loop. They can seem to work because there is often some overlap in queries. Sooner or later they will fail on custom queries.

    If you are not making use of the main post loop, your best option is to alter the main query to meet your needs through the “pre_get_posts” action. Of course this is not an option if your query is a secondary query after the main loop runs.

    For custom queries, you should manage your own pagination. Attempts to fool pagination functions into thinking your query is the main query invariably fail. Managing pagination isn’t as difficult as it might sound. See this Codex page for more information on doing your own pagination: Making Custom Queries using Offset and Pagination

    Thread Starter eadwig

    (@eadwig)

    Hello

    I’m using the same pagination function for the custom post pagination as for the main post pagination and was thinking the offset would not be required due to passing “‘post_type’ => ‘office-magazines'”:

    function pagination_bar( $custom_query ) {
        $total_pages = $custom_query->max_num_pages;
        $big = 999999999; // need an unlikely integer
    
        if ($total_pages > 1){
            $current_page = max(1, get_query_var('paged'));
    
            echo paginate_links(array(
                'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                'format' => '?paged=%#%',
                'current' => $current_page,
                'total' => $total_pages,
            ));
        }
      }

    I read the page, but didn’t understand what type of offset would be required given that I’m using main loops and showing their pagination in separate template pages.

    Could you give me some more advice on this please?

    Thank you,

    Ead

    Thread Starter eadwig

    (@eadwig)

    I’m using pretty much the code as the one given on this page. Despite that, when I click on a page number in the custom post pagination, it doesn’t show the next page for the custom loop and instead WP resorts to showing the home page template.

    Thread Starter eadwig

    (@eadwig)

    For some reason, the link has been dropped off the previous message:

    https://wordpress.stackexchange.com/questions/43489/paginate-custom-post-type-page

    Moderator bcworkz

    (@bcworkz)

    The post type specified isn’t really related to pagination issues, it’s the mixing of main query paging data ($paged) with custom query data. It’s not obvious, but things like get_query_var() and get_pagenum_link() work off of the main query, not your query, so using their return values would likely be invalid part of the time. I don’t know why it worked for the SE respondent, but as you’ve found, it’s unreliable.

    The solution offered in the page I referred you to requires some interpretation. In hooking “pre_get_posts”, you need to ensure you are changing query vars for your custom query. ! $query->is_home() is not enough. There is certainly something unique in the query vars that would identify your query.

    I’m personally not keen to use “pre_get_posts” with custom queries. We can set the desired query vars when we instantiate WP_Query. I would not even bother with paged query var. I advise the use of a custom paging query var, completely separating anything main query related from your custom query. Use the custom query var value to determine your offset. For example, instead of 'format' => '?paged=%#%', use 'format' => '?pg-num=%#%'. Get the pg-num value from $_GET and use it to compute the offset used in your query. Much more straight forward IMO than faffing about with pre_get_posts and paged, etc.

    Be sure to sanitize and validate anything you get from $_GET. If you should choose to use “pre_get_posts” with your custom query for some reason, our pg-num query var will not be available there unless you white list it through the “query_vars” filter. Just add it to the passed array. White listing is not required if you are getting the value directly from $_GET.

    Thread Starter eadwig

    (@eadwig)

    Got the pagination for a custom post loop working with the following code embedded in the :

    $args = array(
    ‘post_type’ => ‘[custom post type name]’,
    ‘paged’ => $paged,
    ‘has_archieve’ => true

    );

    $catquery = new WP_Query($args);

    echo pagination_bar($catquery);

    Hope this helps other developers

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Paginate Custom Post Loop’ is closed to new replies.