Site logo

[Resolved] Pagination not working after pre_get_posts modification

Home Forums Support [Resolved] Pagination not working after pre_get_posts modification

Home Forums Support Pagination not working after pre_get_posts modification

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #2400467
    Jason

    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!

    #2400667
    Ying
    Staff
    Customer Support

    Hi Jason,

    Can you link us to the archive page?

    #2400686
    Jason

    I added two links – one to the archive page and one to the single product page where the pagination is. Thank you

    #2400764
    Fernando
    Customer Support

    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.

    #2400790
    Jason

    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!

    #2400805
    Fernando
    Customer Support

    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

    #2400843
    Jason

    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.

    #2400852
    Fernando
    Customer Support

    You’re welcome Jason!

    #2400883
    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!!

    #2400922
    Fernando
    Customer Support

    You’re welcome Jason! Glad to be of assistance!

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.