I’ve been having a sever and completely unsolvable problem with pagination.
I did notice however this interesting point:
I search a keyword like cute:
?s=cute&submit=Search&post_type=image&paged=2
…is where the link leads to. “Paged=” gives 404 on random pages. But if I modify the URL to say page=
?s=cute&submit=Search&post_type=image&page=2
Every page destination works, bringing joy to my heart, but the pagination tab list always sticks at 0 (not reflecting current page).
I feel like I’m playing a “shell game” with code and settings, with wordpress bamboozling me.
Question reduces to:
How can I get &page=
to be output in every case? Conversely, if &paged=
should be the way it goes, how do I get it to work without 404s?!?!
This is a problem I’ve dealt with for almost 3 months now. People are shy to answer.
update:
Trying to deal with this paged
variable in the URL which breaks pagination, I created a filter to simply replace it with page
.
function symbiostock_pagination_mod( $args ){
$args = str_replace('paged=', 'page=', $args);
return $args;
}
add_filter( 'paginate_links', 'symbiostock_pagination_mod', 1 );
Would seem like a great idea! But then this happens with every new click:
?s=cute&post_type=image&page=2&page=2
?s=cute&post_type=image&page=2&page=3
So where is this being modded, and why?
update:
I’m not sure if I’m the only one that has ever had this problem, but I did solve it (at least for myself) you can see the solution below. Also thanks for the help given. 😀
So here is what I came up with…we might as well call it a hack or work-around because its unknown if this was indeed a bug I’m dealing with (from wordpress itself) or just a perfectly hidden problem in my theme’s code.
The below function filters the ‘
paginate_links()
‘ function and achieves this:I see
paged
variable generates 404 randomly on search pages, but works fine on archive and taxonomy pages. So, we check that.if_search()
, we do our changes…If this is a search, we get the would-be page # destination using regex (or simply string replace
paged
topage
, depending on permalink structure. We useremove_query_var()
to drop faulty variable, andadd_query_var()
to put the working one.This is being done because in this strange case,
page
always generates proper results.Then we return the edited destination link, and life is good.
Function below:
Also thanks to some help I got here with the regular expression: PHP Regular Expression Help: A quick way to extract the 2 from “page/2/”