1

Wordpress is not new for me but I am really stuck on this piece of code that I can not get to work properly.

I register a custom post type News and let the custom post type use the Wordpress categories

'taxonomies' => array('category')

So far so good. I can assign different categories to the custom post type News. But I want that people can see news from a specific category. So I want that to do that with the category.php page. The problem is that the pagination is not working. I get a page not found when I go to /page/2

I use this code on a category.php page.

// Get post ID
$post_type = get_post_type( $post->ID );

// Get category ID
$category_id = get_cat_ID(single_cat_title('', false));

// Wordpress pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

// WP_Query arguments
$args_news = array (
    'post_type'             => array( 'news' ),
    'pagination'            => true,
    'posts_per_page'        => '9',
    'orderby'               => 'date',
    'paged'                 => $paged,
    'cat'                   => $category_id,
);

// The Query
$query = new WP_Query( $args_news );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post(); ?>
        <div>
            <h1><?php the_title(); ?></h1>
            <p><?php the_content(); ?></p>
        </div>
<?php }

} else { 
    // no news found
}
?>

<div class="pagination">
    <div class="previous-page"><?php previous_posts_link( 'Newer Posts' );?></div>
    <div class="next-page"><?php next_posts_link( 'Older Posts', $query->max_num_pages ); ?></div>
</div>

<?php
// Reset postdata
wp_reset_postdata();
1

Before I go to your problem, I would like to start of by asking why you are using a custom query on a category page instead of the main query. You should never change the main query for a custom query on any archive page or on your home page.

Wordpress provides an action hook, pre_get_posts which you can use in conjunction with conditional tags to target and modify the main query before it is executed on specific pages. For category pages the is_category() conditional check is supplied.

You could just simply do the following in your functions.php, while keeping your default loop in tact on your category page. Just a tip, pre_get_posts uses the same parameters as WP_Query

function add_news_to_category( $query ) {
    if ( !is_admin && $query->is_category() && $query->is_main_query() ) {

         $query->set( 'post_type', array( 'news' ) );
         $query->set( 'posts_per_page', '9' );
         $query->set( 'orderby', 'date' );

    }
}
add_action( 'pre_get_posts', 'add_news_to_category' );

OK, now to your actual problem with your code.

  • Firstly, you don't need to use wp_reset_query and wp_reset_postdata together. wp_reset_query is actually used in conjunction with query_posts, which you must never use, and I mean never. Just simply use wp_reset_postdata afterWP_Queryandget_posts`, and remember, never forget to do that

  • Secondly, look closer at your code. You have $query = new WP_Query( $args_news );, but then you have <?php next_posts_link( 'Older Posts', $custom_query->max_num_pages ); ?>. Spot the difference? $query and $custom_query. So, <?php next_posts_link( 'Older Posts', $custom_query->max_num_pages ); ?> should be <?php next_posts_link( 'Older Posts', $query->max_num_pages ); ?>

For further reading:

  • Thank you for the information, it is very useful. The reason I choose a wp_query is that I want two custom post types on different positions on the category page. Maybe load one custom post type with the pre_get_posts method and the other with a wp_query? By the search of the right pagination code I have tried a lot of code. So that is the reason there was still wp_reset_query and the mixup with $query and $custom_query. I have tried your code but it only will load the standard posts of Wordpress not the custom_post_type news. – 3lb3rt Aug 14 '14 at 18:54
  • If you are going to run two different queries and need to paginate that, you are going to run into some trouble. You'll need to merge queries and then paginate it. But that was not in your original question. I would advise you to edit your question, properly explain what you need to do, and add all the relevant code from your affected template – Pieter Goosen Aug 15 '14 at 5:47

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.