0

I seem to have run into a massively common problem, and found about 956 solutions to the issue that didn't seem to knock this one loose.

I have a client with a WP site built on Divi theme. And i implemented a custom post type for them to handle a daily post for their site.

  • Got the CPT done correctly and working fine.
  • Got the custom fields going using the ACF plugin, all good there.
  • Got the custom archive page displaying the posts using wp_query, check.

(worth noting the query is ordered by a custom field that is a future date that is defined by them that is relevant to each post)

They only want 1 post per page, so clicking back and forth through the pagination links would display one at a time. So I used the code below which seems to be a pretty straight forward way of implementing pagination for this purpose, but what happens is , when the "Older Entries" link is clicked it loads a 404 no results found page. It's generating the correct URL structure is seems "ie, website.com/cpt/page/2", but the next post is not displayed on the page.

Any ideas or suggestions would be appreciated.


Code wont paste properly here, link below

https://gist.github.com/colbyalbo/71803505d122004652c024b4baab4a14

  • 1
    That isn't a custom query, that's a replacement query. Have you considered using the pre_get_posts filter to modify the main query instead? – Tom J Nowell Mar 13 '18 at 19:08
  • I didn't consider that , i thought the replacement query was the only way. Reading up on pre_get_posts now. Thanks! – colbyalbo Mar 14 '18 at 1:25
  • The codex states that it doesnt work on template files , but i did get it to work.... using pre_get_posts – colbyalbo Mar 14 '18 at 12:12
  • from the codex "Similarly, pre_get_posts will not work if used in template files (e.g., archive.php), since they are called after the query has been completed. " – colbyalbo Mar 14 '18 at 12:12
  • I am assuming they mean, using it in the template file itself. I put it the code in the functions.php and it worked fine. Limited my admin view also, then a kept reading and got to the section about that , fixed that, and all seems to be good! Thanks! – colbyalbo Mar 14 '18 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.