Programming TipsWordPress Tips

WP-PageNavi Giving 404 Using WP_Query and Custom Post Types: The Fix

Some of our most popular posts here at Ideas and Pixels have their roots in our own frustrations. Whenever we run into a programming problem that seems to have gone undocumented by the masses, we find it appropriate to post the solution for those who weren’t as fortunate as us to find the answer that they need. And like many times before, the answer is simple and is entirely easy to miss.

The WP-PageNavi 404 Page Glitch Fix

Today, while programming a website highly reliant on the use of custom post types and paginated post pages, we ran into an issue that occurs when we clicked subsequent paginated page links (e.g. [2], [3], [next]), we were taken to an ominous 404 page. This issue only happened on pages that used custom post types, while the standard posts “index.php” page worked flawlessly. The solution comes in two parts. We will use “news” as our custom post type in the following examples.

Part 1: Setup Your Post Query With The “Paged” Parameter

'news', 'paged' => $paged,
'posts_per_page' => 4, 'orderby' => 'date', 'order' => 'DESC') );
First, note that you should be using a new WP_Query object as opposed to the alternative query_posts() function. This allows us to pass the WP-PageNavi function the query object to reference, like follows: wp_pagenavi( array('query' => $news) );?> Now, here’s where it got tricky. Theoretically, this should work, but it doesn’t. After some deep searches, we were able to find culprit: permalinks.

Part 2: Updating Your Permalink Settings

From time to time, your .htaccess file that WordPress uses to create its permalinks stops functioning correctly. I’m not an expert at writing .htaccess directives, so I can’t tell you why, but just like a machine that springs to life with a swift kick, sometimes simply navigating to your permalink settings and re-saving your current settings fixes a lot of issues (Settings -> Permalinks). This happens to be one of them. I hope this helps anyone caught in the same bind that we were. Let me know if the issue persists after applying these changes. Happy coding!

Join the discussion One Comment

Leave a Reply

Allen Gingrich

Author Allen Gingrich

Allen has worked on the web for over a dozen years. Like many young entrepreneurs, he began with a small workspace in his basement, where he eventually formed Ideas and Pixels. The rest, well, is history. Allen enjoys fine wines, weight training, and beautiful, semantic code.

More posts by Allen Gingrich

Join the discussion One Comment

Leave a Reply