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.
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. , , [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.
'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.
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!