2

I'm trying to figure out how to fix my pagination problem while filtering out posts on the fly.

I have a dropdown box just above the category archive that allows a user to sort or filter out posts based on custom fields.

The posts, filter just fine on the first page. My problem shows up the minute I filter posts from within 2'nd or deeper page because filtering them out also reduces the number of existing pages. This returns a 404 error for me.

I was wondering how to modify this to also check if the page exists, if not... return to first page.

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

Updated the code as requested:

// ADD SORT QUERY VAR
add_action( 'init', 'sort_rewrite_rules' );
function sort_rewrite_rules() {
    // SORT QUERY VAR
    add_rewrite_tag( '%sort%', '([^/]+)' );
}

add_action( 'pre_get_posts', 'filter_pre_get_posts',11 );
function filter_pre_get_posts( $query ) {
    // DON'T AFFECT WP-ADMIN SCREENS
    if ( is_admin() )
        return;
// SORT ADS FROM THE MAIN QUERY
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$sort = get_query_var( 'sort' );
if ( $query->is_main_query() && $sort ) {


    //EXCLUDE EXPIRED POSTS
    if ( 'expired' == $sort ) {
        $today = date('Y-m-d H:i:s');
        $metaquery = array(
                        array(
                             'key' => 'expire_date',
                             'value' => $today,
                             'type' => 'DATETIME',
                             'compare' => '>'
                    )
            );
        $query->set( 'meta_query', $metaquery );
        $query->set( 'paged', $paged );
    }

}
}


<div id="sort">
    <form action="" method="GET">
        <select name="sort" id="sort-dropdown">

<option value="expired" <?php selected( get_query_var( 'sort' ), 'expired' ); ?>><?php _e( 'Exclude Expired Posts', 'txt' ); ?></option>

   </select>
        <button type="submit" id="sort-submit">Sort</button>
    </form>
    </div>
  • can you add the rest of the code? – Tomás Cot Sep 5 '14 at 22:17
2

You should probably return the user to the first page of results following a search in all scenarios, to be consistent.

If you did need to check though you can use the following:

$wp_query->max_num_pages;

So if there's only 2 pages and you're on page 3 then you need to redirect to the start.

| improve this answer | |
  • Just had a look at this and while it looked valid, I can't seem to return the value for max_num_pages, its always 0. Current page value is returned properly. – ovidiubica Sep 6 '14 at 8:45
  • You'll only have this information to you after the query has been performed. Therefore you'll probably have to do all of this on template_redirect – Steven Jones Sep 6 '14 at 10:20
  • Using template_redirect does pass the max_num_pages value but then I also need to redirect the page itself because the URL is stuck on the same page number. I also need to preserve the query string after redirect. – ovidiubica Sep 6 '14 at 12:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.