0

So my custom wordpress theme/site has a number of venues, e.g. "foo" which has a number of events and reviews.

So a venue ends up with a URL like,

http://www.example.local/places/my-place-name/

But in addition to rendering the venue's content (the_content()... etc) I'm rendering the events and reviews at that venue (the venue is a 'parent' as the event and review post types have the venue as it's 'parent')

So for this I'm running a custom wp_query for these two post types. I'm running into trouble as I can't paginate the results from my custom wp_query. Even if I want to roll my own pagination routine by getting the page number off the URL and then setting the 'offset' on the wp_query, wordpress doesn't like the additional parameters on the end of the URL. For example if I go to:

http://www.example.local/places/my-place-name/mypage/2/

Simply renders a 404 not found. I'd like to avoid using a query string style URL such as

http://www.example.local/places/my-place-name/?mypage=2

As this looks a bit of a mess.

Any guidance on how to make pagination work for the custom wp_query within the post's page (or even how to get wordpress to allow me to add additional pagination parameters to my URL) would be great.

1 Answer 1

1

You need to add a query var and rewrite rule to make that work.

First, add the query var to WordPress query vars array in your theme's functions.php:

add_filter( 'query_vars', 'wpa58904_query_vars' );
function wpa58904_query_vars( $query_vars ){
    $query_vars[] = 'my_page';
    return $query_vars;
}

Then add a rewrite rule with your desired pattern that sets your custom page query var:

add_action( 'init', 'wpa58904_rewrites' );
function wpa58904_rewrites(){
    add_rewrite_rule(
        'places/([^/]+)/mypage/([0-9]+)/?$',
        'index.php?venue=$matches[1]&my_page=$matches[2]',
        'top'
    );
}

Then in your template you can access the value of my_page with

get_query_var( 'my_page' );

Be sure to visit your Permalinks Settings page after adding rewrite rules to flush the rewrites cache.

1
  • That's awesome, thanks,that's exactly what I needed :)
    – gravy
    Jul 19, 2012 at 13:16

Your Answer

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

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