0
votes

I am developing locally on MAMP using "Custom Post Types" and "Custom Fields" plugins. I have a custom post type of "Products" and a custom taxonomy of "Types."

I'm trying to use my pagination in "Taxonomy.php" because that's where I'm going to display my custom post types of "products" with my custom taxonomy "types" and use conditionals to generate different content on that same taxonomy.php.

This is the code in taxonomy.php that I got from css-tricks:

<?php
  $temp = $wp_query; 
  $wp_query = null; 
  $wp_query = new WP_Query(); 
  $wp_query->query('showposts=3&post_type=products'.'&paged='.$paged); 

  while ($wp_query->have_posts()) : $wp_query->the_post(); 
?>

// my content from my custom field that I want paginated

<p><?php the_field('image'); ?></p>

<?php endwhile; ?>

<nav>
    <?php previous_posts_link('&laquo; Newer') ?>
    <?php next_posts_link('Older &raquo;') ?>
</nav>

<?php 
  $wp_query = null; 
  $wp_query = $temp;  // Reset
?>

This question has been asked a lot around the web and I have read at least 30 different articles and blogs about this and still can't seem to find a solution. With this method of pagination I got from css-tricks the only problem is I keep getting a 404 error message whenever I try to go to the next page unless I set my "Blog pages show at most" to 1 in settings > reading. If I do that then it works, otherwise it gives me the 404 page. My permalinks are set to Post Name, by the way.

So I can see my pagination here:

http://localhost/MyWebsite/types/cars/

but when I get here I get the 404 page.

http://localhost/MyWebsite/types/cars/page/2/

I have read about the Flush Method but that does not work. I only have 2 plugins activated (custom post types and fields) and those are not the problem. How can I fix the 404 error?

I have diagnosed the problem further and I figured out whats going on. This is now my code in terminology.php:

<?php
  $args = array(
    'post_type'      => array( 'products' ),
    'posts_per_page' => 5,
    'paged'          => ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1
  );
  $products_query = new WP_Query( $args );

  $temp = $wp_query; 
  $wp_query = null; 
  $wp_query = $products_query; 

  while ($products_query->have_posts()) : $products_query->the_post(); 
?>

// content to display
<p><?php the_field('image'); ?></p>

<?php endwhile; ?>

<nav>
    <?php previous_posts_link('&laquo; Newer') ?>
    <?php next_posts_link('Older &raquo;') ?>
</nav>

<?php 
  $wp_query = null; 
  $wp_query = $temp;  // Reset
?>

Bear with me because this is a little bit confusing. I'll try my best to clearly explain the difference between "Blog pages show at most" in settings > reading and my posts_per_page arguments.

I have a category of Honda with a sub-term of Accord and I have 3 products of my custom post type assigned to Accord

Now if I changed my posts_per_page argument what that does is no matter how many "Products" I have under "Accord" it will always demonstrate that amount even if you do not have enough.

So if I set it to 5 and I have 3 products it will still show 5 either repeated or from another category. When I click on the next pagination the pages will only go as far as however many items that I have. That is if "Blog pages show at most" is set to 1, which brings me to my next point.

Now the difference in changing "Blog pages show at most" is that even if I have my posts per pages set to 5 and I have only 3 "Products" but I change my "Blog pages show at most" to 3 it will not go to the next page because I only have 3 "products" in accord term.

If I were to change my "Blog pages show at most" to 1, I would be able to go forward 3 pages because I have 3 items.

It's sort of like changing "Blog pages show at most" changes the core of WordPress so no matter how many you put to display in the posts_per_page args it will only really count however many you set in "Blog pages show at most".

It seems like the pagination count is based upon "Blog pages show at most", not posts_per_page. I can have posts per page set to 10 and "Blog pages show at most" set to 2 and it would show 10 but only count as if there were really 2 on each page meaning I would only be able to go to the 2nd page of my "Accord" term that has 10 displaying in both of them.

Get the pattern? WordPress counted 2 products per page even though it was displaying 10. So it told the pagination that it can't go past page 2 because I only have 3 products.

1

1 Answers

0
votes

But if you have a custom post type products and a taxonomy types you will show your content on: archive-products.php and taxonomy-types.php you could use pre_get_posts action to modify the query if you need and then write the markup you want on those files, example:

Put this on your functions.php

<?php 
  add_action( 'pre_get_posts', 'mr_modify_archive_taxonomy_query' );

  function mr_modify_archive_taxonomy_query( $query ) {
    if ( !is_admin() && $query->is_main_query() ) {
      if ( is_post_type_archive( 'products' ) || is_tax( 'types' ) ) {
        $query->set( 'posts_per_page', 3 );
      }
    }
  }
?>

an use a default loop

<?php
if ( have_posts() ): 
    while ( have_posts() ): the_post();
        printf( '<p>%s</p>', get_the_field( 'image' ) );
    endwhile;
    previous_posts_link('&laquo; Newer');
    next_posts_link('Older &raquo;');
endif;
?>