1

I'm at the end of my tether now. Really need some help! Here's the setup...

I have 2 custom post types in my functions.php:

// Register the events post type
function events() {
    $labels = array(
        'name' => _x('Evenemang', ''),
        'singular_name' => _x('Evenemang', ''),
        'menu_name' => _x('Evenemang', ''),
        'name_admin_bar' => _x('Evenemang', ''),
        'add_new' => _x('Skapa ny', 'evenemang'),
        'add_new_item' => __('Skapa nyt evenemang'),
        'search_items' => __('Sök evenemang'),
        'not_found' => __('Hittade inga evenemang.'),
        'not_found_in_trash' => __('Inga evenemang hittades i papperskorgen.'),
    );
    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_in_menu' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'evenemang'),
        'query_var' => 'evenemang',
        'menu_icon' => 'dashicons-calendar-alt',
        'supports' => array('title', 'editor', 'thumbnail')
    );
    register_post_type('evenemang', $args);
}
add_action('init', 'events');

// Register the recipes post type
function recipes() {
    $labels = array(
        'name' => _x('Recept', ''),
        'singular_name' => _x('Recept', ''),
        'menu_name' => _x('Recept', ''),
        'name_admin_bar' => _x('Recept', ''),
        'add_new' => _x('Skapa ny', 'recept'),
        'add_new_item' => __('Skapa ny recept'),
        'search_items' => __('Sök recept'),
        'not_found' => __('Hittade inga recept.'),
        'not_found_in_trash' => __('Inga recept hittades i papperskorgen.'),
    );
    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_in_menu' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'recept'),
        'query_var' => 'recept',
            'menu_icon' => 'dashicons-carrot',
        'supports' => array('title', 'thumbnail')
    );
    register_post_type('recept', $args);
}
add_action('init', 'recipes');

For each of these 2 I have an archive-{post_type}.php, and i'm using <?php echo paginate_links(); ?> to spit out the pagination links, here's archive-evenemang.php:

<?php get_header(); ?>

    <div class="list">
        <?php
        $today = date('Ymd');
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = array(
            'posts_per_page' => 8,
            'post_type' => 'evenemang',
            'meta_key' => 'event_date',
            'orderby' => 'meta_value_num',
            'order' => 'ASC',
            'paged' => $paged,
            'meta_query' => array(
                array(
                    'key' => 'event_date',
                    'compare' => '>=',
                    'value' => $today
                )
            )
        );
        ?>
        <?php $posts = get_posts($args); foreach ($posts as $post) : setup_postdata($post); ?> 
            <h1><?php the_title(); ?></h1>
        <?php endforeach; wp_reset_postdata(); ?>
    </div>
    <div class="pagination">
        <?php echo paginate_links(); ?>
    </div>

<?php get_footer(); ?>

and here's archive-recept.php:

<?php get_header(); ?>

    <div class="list">
        <?php
        $today = date('Ymd');
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = array(
            'posts_per_page' => 8,
            'post_type' => 'recept',
            'orderby' => 'title',
            'order' => 'ASC',
            'paged' => $paged
        );
        ?>
        <?php $posts = get_posts($args); foreach ($posts as $post) : setup_postdata($post); ?> 
            <h1><?php the_title(); ?></h1>
        <?php endforeach; wp_reset_postdata(); ?>
    </div>
    <div class="pagination">
        <?php echo paginate_links(); ?>
    </div>

<?php get_footer(); ?>

As you can see they are set up exactly the same in functions.php, and use the basically the same archive template (with a minor adjustment to the $args in the query).

The "evenemang" page works perfectly, it shows the results, the pagination buttons and i can access mydomain.com/evenemang/page/2/.

The "recept" page also works, it shows the results, BUT no pagination buttons and if i try and access mydomain.com/recept/page/2/ it just goes to the front page.

Why does it work for one and not the other? Do i need to change some args inside the paginate_links() function? A problem with using get_query_var('paged') on multiple archives? Is it a .htaccess problem?

1 Answer 1

1

Your problem comes from a fundamental misunderstanding of how WordPress loads the main loop.

Here you have a main query that goes and grabs the posts to display in your post type archive. It then decides to load archive-recept.php based on that query.

The call to paginate_links then provides the pagination for that main query. However, the main query isn't being used.

Instead what you're doing is ignoring the main query, calling your own query ( doubling the DB work involved ), then expecting the pagination links to give you pagination for your get_posts query, rather than the main query.

This is functionally equivalent to calling query_posts, which as we know is bad practice.

Instead, use the pre_get_posts filter to modify the main query before it happens, e.g. something similar to:

function wpse182971( $query ) {
    if ( $query-> is_post_type_archive && $query->is_main_query() ) {
        $query->set( 'posts_per_page', '8' );
    }
}
add_action( 'pre_get_posts', 'wpse182971' );
1
  • Thanks for the quick/thorough reply dude, you are a lifesaver. Commented Apr 1, 2015 at 21:43

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.