1

I am creating category and tag page templates for my client so they can post news articles to the news category page and specific news pages separated by tag with a pagination nav at the bottom of each page. What I noticed, however, is the pagination nav takes account of all of the posts belonging to any category or having any tag.

I am aware that paginate_links() does not pass an argument in any way similar to the category_name argument for WP_Query(). Keeping this in mind, I spent Googling for a solution to excluding all of the irrelevant posts from the scope of paginate_links() with no luck.

Can you write paginate_links() so that the function targets a specific category or tag? If so, please help me work out how.

The below is the code including the query and pagination function:

<?php
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        $args = array(
            'post_type' => 'post',
            'post_status'=>'publish',
            'category_name' => 'NEWS',
            'tag_slug__and' => $tags,
            'posts_per_page' => 2,
            'paged' => $paged
        );
    $the_query = new WP_Query($args); ?>

<?php if ( $the_query->have_posts() ) :  ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post();  ?>
            <div class="inner">
                <?php the_content(); ?>
            </div>
    <?php endwhile; ?>

    <div class="pagination">
        <?php
            echo paginate_links( array(
                'format'  => 'page/%#%',
                'current' => $paged,
                'total'   => $the_query->max_num_pages,
                'mid_size'        => 2,
                'prev_text'       => __('&laquo; Previous Page’),
                'next_text'       => __(‘Next Page &raquo;')
            ) );
        ?>
    </div>

<?php endif; ?>

Thank you for reading this.

Ead

  • You dont need to pass custom query to paginate_links(). Custom query will suffice I guess. See this example - developer.wordpress.org/reference/functions/paginate_links/… – Nilambar Jun 12 at 6:31
  • 1
    paginate_links() only paginate the query's results; you can use the pre_get_posts hook (particularly for the main query) to control the query arguments or make a custom WP_Query request where you have full control over the query arguments. So for example, you can do new WP_Query( 'category_name=foo' ) and then call paginate_links(), and the pagination wouldn't "include" posts that are not in the foo category. – Sally CJ Jun 12 at 6:32
  • 1
    It sounds like you're creating these templates incorrectly. When you create categories they automatically get pages where you can view that category's posts. You just need to build your templates correctly. What do your templates look like? – Jacob Peattie Jun 12 at 6:36
  • Sally CJ's comment makes sense, but I seem to have paginate_links() inside the custom query already as follows: $the_query = new WP_Query($args); if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); endwhile; echo paginate_links( array( 'format' => 'page/%#%', 'current' => $paged, 'total' => $the_query->max_num_pages, 'mid_size' => 2, 'prev_text' => __('&laquo; Next Page'), 'next_text' => __('Previous Page&raquo;') ) ); endif; – Ead Wig Jun 12 at 7:11
  • Why are you doing $the_query = new WP_Query($args);? That should not be required in category templates. – Jacob Peattie Jun 12 at 7:24
0

The tag template page began displaying posts for specific tags once I replaced the argument statement with the following, which is great!!:

$tags = get_the_tags();
$tag = $tags[0]->name;

$args = array(
    'post_type' => 'post',
    'post_status'=>'publish',
    'category_name' => 'NEWS',
    'tag_slug__and' => $tag,
    'posts_per_page' => 2,
    'paged' => $paged
);

However, when I click on a pagination link on the tag archive page, Chrome returns 'This localhost page can’t be found'...

  • Actually, that error 404 is likely because of conflict with the main query on the category archive page. You'd need to tweak the paginate_links() format. – Sally CJ Jun 13 at 8:02
  • The pagination links seem to be showing correct pretty URLs such as /category/news/page/2. So as @SallyCJ pointed out, the cause of this problem is the way paginate_links() is set up? I can't spot any anomaly with the function... – Ead Wig 2 days ago
  • When I said "conflict", I mean something like this: Let's say your category is foo, and the main query had 3 pages, while the custom query had 4 (or more) pages. So you visited /category/foo/page/2 (the main query) and it worked well. Then you visited /category/foo/page/4 (the custom query), but got the error 404. Why? Because WordPress uses the main query with the requested URL and since the main query had only 3 pages of results, that's why the error 404 came up despite that you're actually navigating through the custom query's results. – Sally CJ 2 days ago
  • And you could overcome the issue by tweaking the pagination links (their URL format) - use an "ugly" URL and use my_paged as the page number's key (the default is paged). But is it really necessary for you to make the custom query in the category template?? :) How about creating a standard Page (post type of page) and make your custom queries there (in a custom Page template)? – Sally CJ 2 days ago

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.