Archived topic
Pagination not working after pre_get_posts modification
9 replies · Started by Jason on November 3, 2022
Hey y'all. I recently modified my custom post archives to display alphabetically and it threw my custom single-post pagination off. What am I doing wrong? I'm sure I need to add another filter for the pagination, but not sure the right way to do it. The pagination is a block element set to "post navigation" and then container links are dynamic set to "next" and "previous" posts, respectively.
Here is the code I used to modify the archive page
function foo_modify_query_order( $query ) {
if ( ! is_admin() && $query->is_main_query() && ! $query->is_post_type_archive('happenings') ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
}
add_action( 'pre_get_posts', 'foo_modify_query_order' );
Any help is appreciated, as always. Thank you!
Hi Jason,
Can you link us to the archive page?
I added two links - one to the archive page and one to the single product page where the pagination is. Thank you
Hi Jason,
May we know what exactly is occurring with the pagination? I tried testing the pagination from my end, and it seems to be working as expected.
Let us know.
Hi Fernando. It appears to be working with some products, but it's not. I included a single product page that starts with W and the "next" pagination tab goes to an H (archive page is alphabetical, but single post pagination is off). Thanks for looking!
I see. That code you have only works for archive pages. The single post pagination is a bit more complicated.
It uses WordPress function: previous_post_link and next_post_link
References:
https://developer.wordpress.org/reference/functions/previous_post_link/
https://developer.wordpress.org/reference/functions/next_post_link/
These two functions retrieve posts ordered by date. It's not possible in these functions to change the order. You would need to alter the Query of the posts inherently. Altering WordPress core would be out of our scope. See: https://generatepress.com/what-support-includes/
I saw this thread however which might help though: https://stackoverflow.com/questions/9925035/wordpress-previous-post-link-next-post-link-by-alphabetical-order
I understand, and thank you for the references, Fernando! I appreciate the time. I'll look into it and see if I can hack something together.
You're welcome Jason!
Fernando - That lead to stack overflow was money! I added a snippet and a custom hook and I have officially modified my single post navigation! Thank you again!!
You're welcome Jason! Glad to be of assistance!