Support » Fixing WordPress » Custom post and category template pagination problem

  • Hi guys,

    i have a strange problem in my wordpress. I have a category template wich holds a query to custom post with a the category. The strange thing is when the pagination loads it displays how many pages we have. But when i click on the second page of the pagination it gives me a 404 page. If I make the query only to the custom post or to the category it works fine. After i have refresh the query with the test for the custom post and the category it works fine. Here is the code of the category template:

    <div id="conteiner-first-vision">
      <section id="inner-pad">
     	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$args = array(
    	'posts_per_page' => 6,
    	'post_type' =>  'awards-pub',
    	'paged' => $paged,
    	$wp_query = new WP_Query( $args );
    	while ( $wp_query->have_posts() )
        <article class="article-first-vision <?= ($i==2)?'mar':'' ?>" >
          <?php if ( has_post_thumbnail()) { ?>
          <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
          <?php the_post_thumbnail('menu-pub'); ?>
    	}; ?>
            <?php the_title(); ?>
          <div class="article-description">
            <?= truncateHtml(get_the_content()); ?>
          <a href="<?php the_permalink(); ?>" style="bottom:0; right:0;" class="pa"><img src="<?php bloginfo('template_url') ?>/img/link-articles.png"></a> </article>
      <div class="clear"></div>
      <div class="pagination-conteiner">
      <div class="clear"></div>

    And this is the pagination:

    global $wp_query;
        $big = 999999999;
        echo paginate_links(array(
            'base' => str_replace($big, '%#%', get_pagenum_link($big)),
            'format' => '?paged=%#%',
            'current' => max(1, get_query_var('paged')),
            'total' => $wp_query->max_num_pages,
    		'prev_next'    => True,
    		'prev_text'    => __('«'),
    		'next_text'    => __('»'),

    and this is the custom post:

    register_post_type('awards-pub', // Register Custom Post Type
            'labels' => array(
                'name' => __('Awards', 'awardspub'), // Rename these to suit
                'singular_name' => __('Awards Post', 'awardspub'),
                'add_new' => __('Add Awards', 'awardspub'),
                'add_new_item' => __('Add Awards  Post', 'awardspub'),
                'edit' => __('Edit', 'awardspub'),
                'edit_item' => __('Edit Awards Post', 'awardspub'),
                'new_item' => __('New Awards Post', 'awardspub'),
                'view' => __('View Awards Post', 'awardspub'),
                'view_item' => __('View Awards Post', 'awardspub'),
                'search_items' => __('Search Awards Post', 'awardspub'),
                'not_found' => __('No Awards Posts found', 'awardspub'),
                'not_found_in_trash' => __('No Awards Posts found in Trash', 'awardspub')
            'public' => true,
            'hierarchical' => true, // Allows your posts to behave like Hierarchy Pages
            'has_archive' => true,
            'supports' => array(
            ), // Go to Dashboard Custom HTML5 Blank post for supports
            'can_export' => true, // Allows export in Tools > Export
            'taxonomies' => array(
            ) // Add Category and Post Tags support
    And to add the category to the custom post:

    register_taxonomy_for_object_type(‘category’, ‘awards-pub’);
    register_taxonomy_for_object_type(‘post_tag’, ‘awards-pub’);

Viewing 7 replies - 1 through 7 (of 7 total)
  • I have found that it start to work if i go to the posts and make more then the post_per_page post with that category.

    Moderator keesiemeijer



    Try it without the query in the category template, but with a query in your functions.php:

    function my_post_queries( $query ) {
    	// not an admin page and is the main query
    	if ( !is_admin() && $query->is_main_query() ) {
    		// use post type awards-pub on category 6 pages
    		if ( is_category( 6 ) ) {
    			$query->set( 'posts_per_page', 6 );
    			$query->set( 'post_type', array( 'awards-pub' ) );
    add_action( 'pre_get_posts', 'my_post_queries' );

    The point is that when it has only the category it works fine but when we include the custom posts it has the problem. keesiemeijer i will try this. Thanks!

    No this generate the same thing… What can be the problem? I did not understand when i create post in the posts and attach to them the category it works really fine…

    The same template i have duplicated in the page template and it works perfekt.

    I also want to point that the template is category-slug.

    Hi this is the solution to my problem

    add_action( ‘parse_query’,’changept’ );
    function changept() {
    if( is_category() && !is_admin() )
    set_query_var( ‘post_type’, array( ‘menu-pub’, ‘new-pub’, ‘read-pub’, ‘awards-pub’, ‘live-pub’,’post’ ));

    And put it in the functions file.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom post and category template pagination problem’ is closed to new replies.