2

Ok, so I know it's a silly title, but it's exactly how I feel right now..

I've created some custom rewrites for WordPress and they work great, the only problem is that now I need to add pagination. My rules are messing with the pagination and vice versa. The page url will update when I click on a pagination link, but the query stays the same..

Here's what I have in my functions.php

// ADD REWRITE FOR VIDEOS -> GROUP
add_rewrite_tag('%group%','([^&]+)');

// ADD REWRITE FOR VIDEOS -> VALUE
add_rewrite_tag('%value%','([^&]+)');

// ADD REWRITE FOR VIDEOS TO ACCEPT GROUP & VALUE
add_rewrite_rule('^car-videos/group/([^/]*)/value/([^/]*)/?','index.php?page_id=40&group=$matches[1]&value=$matches[2]','top');

Here's my pagination code

<div class="pagination">
                            <?php $total_pages = $loop->max_num_pages;  

                                    if ($total_pages > 1){  
                                    $current_page = max(1, get_query_var('paged'));  

                                    echo paginate_links(array(  
                                            'base' => get_pagenum_link(1) . '%_%',  
                                            'format' => 'page/%#%',  
                                            'current' => $current_page,  
                                            'total' => $total_pages, 
                                            'prev_text'    => __('Previous'),
                                            'next_text'    => __('Next')
                                        ));  
                                    }   
                            ?>
                        </div>

Here's how I'm getting the group and value variables

$urlGroup = $wp_query->query_vars['group'];
$urlValue = $wp_query->query_vars['value'];

I use this in my WP_Query

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

How would I rewrite the rule to allow for pagination? Is there something I'm doing wrong?

1 Answer 1

4

I'm not sure but I think that you just need to add two query vars, "group" and "value", not two rewrite tags. Can you try this:

add_filter('query_vars', 'cyb_add_query_vars');
function cyb_add_query_vars( $vars) {
    $vars[] = "group"; // name of the var as seen in the query string and URL
    $vars[] = "value";
    return $vars;
}
add_action('init','cyb_add_rewrite_rules');
function cyb_add_rewrite_rules() {
    add_rewrite_rule( '^car-videos/group/([^/]*)/value/([^/]*)/?$','index.php?page_id=40&group=$matches[1]&value=$matches[2]','top');
    //Rule for pagination
    add_rewrite_rule( '^car-videos/group/([^/]*)/value/([^/]*)/page/([0-9]{1,})/?$', 'index.php?page_id=40&group=$matches[1]&value=$matches[2]&paged=$matches[3]', 'top' );
}

Then this should work for pagination:

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

And you can get the values of "group" and "value" with this:

$group = get_query_var('group');
$value = get_query_var('value');
//OR
global $wp_query;
$group = $wp_query->get( 'group' );
$value = $wp_query->get('value');
3
  • It seemed to be rewriting the videos permalink to go to the home page.. BUT the fix just had a few typo's in it preventing it from working correctly. Thank you so much! Commented Aug 26, 2014 at 20:24
  • Please, edit the answer to fix the typo's. I can't test the code right now.
    – cybmeta
    Commented Aug 26, 2014 at 20:31
  • It appears to be fixed, or something weird happened when I copy and pasted because the function names didn't match the function names in the filter/action. Commented Aug 27, 2014 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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