PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 8, 2022

[FIXED] How to get pagination working with “Pending” Posts

 July 08, 2022     archive, categories, pagination, posts, wordpress     No comments   

Issue

I'm working on a site that allows users to browse through pending posts/content on the front-end.

However, I can't seem to get pagination working with those posts. I've got a custom query that brings up the first page of pending posts on a category page, archive, etc.

But the Page 2, 3, etc. doesn't work.

Any thoughts?

Thanks!

Here's the example code I'm working with:

$args = array(
    'cat'      => $cat_ID,
    'paged' => get_query_var('paged'),
    'showposts' => 50,
    'posts_per_page' => 50,
    'post_status' => 'pending',

);

query_posts($args);

if( have_posts() ) : while (have_posts()) : the_post();

//Post code inserted here

<?php endwhile; ?>
<?php endif; ?>

Solution

WordPress pagination will 404 if there are not enough results in the main query to run to that page.

I'm sure there is a better way, but the only way I can think of round it is to use your custom loop in an archive/search page that has more posts than you have drafts.

For example, add your custom loop to the search.php template and reach it by passing a search query that will generate lots of results (e.g. 'a'). Using search.php will include pages, posts and custom post types in the results so will yield more results.

Use paginate_links for the pagination, which will continue to pass the queryvar on each page and after that you should be good.

On a slightly separate note, I would suggest using WP_Query instead of query_posts. See here for why.



Answered By - Mark
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing