• Resolved zworks

    (@zworks)


    Hi, i will really appreciate your help on this, because i have already spent two days on resolving this issue without any luck.

    When i visit the custom taxonomy page i see 8 posts, but when i click on the pagination to see the 2nd page i get 404 error.

    Following are the settings:

    In the taxonomy edit page:
    Taxonomy slug = projectcat
    Custom Rewrite Slug = renoinspiration

    I have a term with the slug “cuisine”

    When i visit /renoinspiration/cuisine/ It’s fine, it lists the 1st set of posts with this term.
    But when i visit /renoinspiration/cuisine/page/2/ i get the 404 error.

    Also tried to add the following rewrite rule but with no luck.

    add_rewrite_rule('^renoinspiration/([^/]*)/page/([^/]*)$', 'index.php?paged=$matches[2]&projectcat=$matches[1]', 'top');

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Are you trying to do a custom term archive listing with pagination? If so, this may be of good relevance https://docs.pluginize.com/article/81-cleanly-done-pagination-with-custom-wpquery-objects

    Also just to be certain, and I assume you have since you’re using add_rewrite_rule, but have you flushed the rewrite rules from the permalinks page?

    Thread Starter zworks

    (@zworks)

    yes, every time i change change/add rewrite rules i flush the rewrite rules.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    What about my first question?

    Thread Starter zworks

    (@zworks)

    Well, if i understand correctly, i think the link you provided mainly focuses on how to do a clean pagination, but my issue is not with generating the pagination, the pagination links are already generated correctly and exactly how it should be, but when we click the 2nd page or other pages it gives a 404 error.

    In the rewrite rule, it works fine only when there is no ‘paged’ variable, but if we add the ‘paged’ variable then it gives the 404 error.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    To be technical, the question was actually about what type of query was being made. To get into specifics, if you were doing something with $foo = new WP_Query( $args ); or similar, and trying to offer pagination with the resulting output. The link provided does have to do with those cases.

    However, if you’re just trying to get a custom taxonomy term archive similar to category terms or standard blog post archives, then you should be able to set that up in your CPTUI settings and not have to worry about extra details there.

    Really depends on exactly what you’re trying to do and how.

    Thread Starter zworks

    (@zworks)

    As we have stated in my original question, we have a taxonomy called projectcat and we have a template file named taxonomy-projectcat.php

    And we have a term called cuisine belonging to the projectcat taxonomy.

    The custom url rewrite slug in the taxonomy settings page is renoinspiration

    So whenever we visit /renoinspiration/cuisine it loads the taxonomy-projectcat.php template and shows fine, means it shows the 8 posts, because we do the query as following:

     $paged = (get_query_var('page')) ? get_query_var('page') : 1;
                $wp_query = new WP_Query();
                $wp_query->query(array(
                'post_type'=>'projects',
                'paged' => $paged,
                'posts_per_page' =>8,
                ));
               	  
                  $args_page = array('post_type' => 'projects', 'tax_query' => array(array('taxonomy' => 'projectcat', 'field' => 'id','terms' => $term_id)), 'post_status' => 'publish', 'order' => 'DESC',  'posts_per_page' => 8, 'paged' => $paged,  'total' => $wp_query->max_num_pages);
                  $loop = new WP_Query( $args_page );

    And at the end we have the following:

    <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } else { ?>
    				<div class="alignright"><?php previous_posts_link('Next Entries &rarr;') ?></div>
    		        <div class="alignleft"><?php next_posts_link('&larr; Previous Entries') ?></div>
    		        
    		        <?php } ?>

    If we have more than 8 posts of this type, it’s fine on the 1st page and displays the pagination correctly, but when we click on the other links on the pagination we get a 404.

    We really appreciate your time!

    • This reply was modified 8 years, 4 months ago by zworks.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Thanks for that extra code. At this point I’m pretty certain that it’s not a rewrite rule issue.

    Based on the provided code, I’m seeing some red flags. As a general rule, I always avoid the variable name $wp_query since WordPress already uses it for its default global posts object for the blog/archives/page display. Overwriting it will most likely cause some havoc.

    You’re also doing multiple WP_Query calls, which seems really odd, especially when one seems to be trying to set the total property yourself. That’d be one that I leave to the WP_Query class to set itself. It’s already going to know how many total it has.

    From what I understand, and what has been confirmed with some quick testing, is that you should not need to do all that much to get the type of listing you’re appearing to want. I set up a basic “tv_show” post type, and a “style” taxonomy. Then created three tv_show post type posts, assigned all the same term “Soap Opera”, and published. Afterwards, I reduced my posts per page to 1, and went to https://wds.dev/blog/style/soap-opera/. Don’t mind the /blog/ I happen to be testing with a multisite install. When I clicked on the pagination options at the bottom, it took me to https://wds.dev/blog/style/soap-opera/page/2/ and showed the next post, no issues.

    It feels like you’re trying to do more than you actually need to, in the end. If you’re trying it this way because you want to change a minor detail to the query, say you only want 8 posts per page, instead of 10 for other parts of the site, there’s better ways to handle that, via the pre_get_posts hook.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Any changes or followup for this one @zworks ?

    Thread Starter zworks

    (@zworks)

    well, i got my original coding working by just changing the ‘paged’ variable into ‘page’. But i don’t know how that works!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Custom taxonomy pagination not working’ is closed to new replies.