In that last block, there’s a mix of bracketed and bracket-free if statements that make it hard to debug. I’d suggest rewriting it like this and using indentation to make it easier to see what is going on at a glance:
if (is_page(esc_url( wp_get_current_url() . '/?sortby=asc'))) {
if ( $newQuery->have_posts() ) {
while ( $newQuery->have_posts() ) {
$newQuery->the_post();
}
}
} else {
if (is_page(esc_url( wp_get_current_url() . '/?sortby=desc'))) {
if ( $newQuery->have_posts() ) {
while ( $newQuery->have_posts() ) {
$newQuery->the_post();
}
}
} else {
if (have_posts()) {
while (have_posts()) {
the_post();
}
}
}
}
An editor with syntax highlighting, like VSCode for example, also makes this a lot easier — it changes pairs of bracket colors so that they match.
There are some other problems in the code, though:
- As @ddur mentioned
is_page()
is also used incorrectly, you can see this answer for more details (which you’ve already seen). - You could use
else if
to simplify these nestedif
statements quite a lot and make it easier to read - Those echo statements don’t work the way you think, see the code above instead