0

I'm bringing up an issue that could not be solved after googling for solutions over a few days. The goal I have to meet is to output a pagination for a custom post type named office-magazines with working links. I already managed to output the pagination but clicking any of the links within it takes me to the top page.

Here's the code I embedded into the page template:

   <?php
        global $wp_query, $paged;
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

        $args = array(
          'post_type' => 'office-magazines',
          'posts_per_page' => 9,
          'paged'          => $paged,
          'has_archieve'  => true
        );

        $catquery = new WP_Query($args);
   ?>

   <p class="pagination">
    <?php echo custom_pagination_bar( $loop ); ?>
  </p>

Passing 'post_type' => 'office-magazines' is meant to filter out all posts apart from the ones belonging to the custom post type, 'office-magazines'.

Using the next code in functions.php, I intended to define the function of the pagination:

function custom_pagination_bar($custom_loop) {
  $big = 999999999;
 echo paginate_links( array(
    'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $custom_loop->max_num_pages
) );
}

The same code works for the main post pagination which is output in a separate template page but fails for the custom post pagination.

Could someone please help me find a solution to getting the custom post pagination links to work?

Hoping for some advice,

Ead

  • Any reason you're using a custom query and not the main loop? – Jacob Peattie Apr 12 '18 at 7:18
  • 1
    the variable you are passing is wrong here.. it should be $catquery <?php echo custom_pagination_bar( $catquery ); ?> – Aishan Apr 12 '18 at 7:26
  • Indeed, I did not have to resort using the custom query: I managed to filter the main query for the custom post type in the end. – Fizzler Apr 17 '18 at 8:13
0

Use the following:

$args = array(
  'post_type' => '[custom post type name]',
  'paged' => $paged,
  'has_archieve' => true    
);
$catquery = new WP_Query($args);    
echo pagination_bar($catquery);
  • Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Apr 17 '18 at 14:49
0

The only solution that worked for me happens to add an action filter to functions.php in the following format:

add_action('pre_get_posts', function($q){
    if (is_category()){
        if(is_category('NEWS')):
            $q->set( 'posts_per_page', 2 );
        endif;
    }
});
  • You can delete first is_category(). – nmr Jun 12 at 6:43
  • The code pasted above was set to be expanded for other category pages and a standard category page, hence having the extra is_category(). – Fizzler Jun 12 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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