0

So, been reading tons of posts on here and int he Codex but just not having any luck. I have a custom post type called Work. On my portfolio page I have "posts_per_page" set to 12. In total in this custom post type I have 300 posts. I am trying to get paging working but not getting it at all.

My query:

        <?php
            $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
            $all_work = new WP_Query(array(
                'post_type' => 'work',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'type',
                        'field' => 'slug',
                        'terms' => array('case-study', 'portfolio')
                    ),
                ),
                'posts_per_page' => '12',
                'orderby' => 'title',
                'order' => 'ASC',
                'update_post_term_cache' => false,
                'nopaging' => false,
                'paged' => 10
                )
            );
            while ( $all_work->have_posts() ) : $all_work->the_post();
        ?>

My code for the pagination:

                                        <aside id="post_nav">
                        <?php                   
global $wp_query;

$total_pages = $wp_query->max_num_pages;

if ($total_pages > 1){

  $current_page = max(1, get_query_var('paged'));

  echo '<div class="page_nav">';

  echo paginate_links(array(
      'base' => get_pagenum_link(1) . '%_%',
      'format' => '/page/%#%',
      'current' => $current_page,
      'total' => $total_pages,
      'prev_text' => 'Prev',
      'next_text' => 'Next'
    ));

  echo '</div>';

}

                        ?>              
                    </aside><!-- /#post_nav --> 

Not getting anything though. Any help would be greatly appreciated!

Thanks!

4
  • possible duplicate of Having trouble generating pagination links on custom query
    – s_ha_dum
    Commented Sep 29, 2013 at 18:49
  • I looked over that thread as well before posting and the solution doesn't seem to apply with my scenario. I have tried the suggested answer numerous times but no luck =/
    – Danny
    Commented Sep 29, 2013 at 19:04
  • OK... "Not getting anything though." Statements like that are almost always false. You must be getting something. What?
    – s_ha_dum
    Commented Sep 29, 2013 at 19:12
  • @s_ha_dum, the container for the paging isn't coming up with any results. So no paging is actually showing up. I added: <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div> <div class="alignright"><?php next_posts_link('More &raquo;') ?></div> and changed my loop to be: 'paged' => $paged
    – Danny
    Commented Sep 29, 2013 at 19:19

1 Answer 1

2

You appear to be trying to paginate the $all_work query data, but your pagination code is using the $wp_query data...

global $wp_query;

$total_pages = $wp_query->max_num_pages;

if ($total_pages > 1){

  $current_page = max(1, get_query_var('paged'));

... for reasons unknown to me.

If you want to paginate $all_work you need to use the $all_work data. The data in $wp-query is not going to be correct. I suspect it is this mixing of queries that is causing the trouble, and I suspect a similar mixing up of queries that is the reason the code in suggested duplicate isn't working for you.

Something like this should work:

$page = (!empty($_GET['myp'])) ? $_GET['myp'] : 1;
$per_page = 1;
$base = get_queried_object();
// var_dump($base);
if (null === $base) {
  $base = site_url();
} else {
  $base = get_permalink($base->ID);
}

// $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$all_work = new WP_Query(array(
    'post_type' => 'page',
//  'tax_query' => array(
//      array(
//          'taxonomy' => 'type',
//          'field' => 'slug',
//          'terms' => array('case-study', 'portfolio')
//      ),
//  ),
    'posts_per_page' => $per_page,
    'orderby' => 'title',
    'order' => 'ASC',
    'update_post_term_cache' => false,
    'nopaging' => false,
    'paged' => $page
  )
);

$total = $all_work->found_posts;
var_dump($total);

while ( $all_work->have_posts() ) {
  $all_work->the_post();
  the_title(0);
}

$page_args = array(
    'base'         => $base.'%_%',
    'format'       => '?myp=%#%',
    'total'        => ceil($total / $per_page),
    'current'      => $page,
    'show_all'     => True,
    'end_size'     => 1,
    'mid_size'     => 1,
    'prev_next'    => True,
    'prev_text'    => __('« Previous'),
    'next_text'    => __('Next »'),
    'type'         => 'plain',
);

echo '<br/>';
echo paginate_links($page_args);

That is generic code, as I don't have your data on my server, but it should be obvious what needs to be changed.

1
  • Awesome! Thank you so much @s_ha_dum... appreciate you taking the time to break it down for me.
    – Danny
    Commented Sep 29, 2013 at 20:38

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.