1

Currently, I am trying to create an alphabetic pagination with ranges for a custom post type on a custom field using this code from kathy is awesome. I have gotten everything working except for the range aspect of it. Such as A-E, F-H and so on. Any help would be great. Thanks.

1

If you have used that code, now you have a custom taxonomy called 'glossary', what you need to show the range is a custom query like

new WP_Query( array(
  'tax_query' => array(
     array(
       'taxonomy' => 'glossary',
       'field' => 'slug',
       'terms' => range('a', 'e')
     )
   )
) );

The problem is how to perform this query with a url?

You can make use of an endpoint, something like

add_action('init', 'add_glossary_range_endpoint');

function add_glossary_range_endpoint() {
    add_rewrite_endpoint( 'letters', EP_ROOT );
}

Doing so, when you visit the link http://example.com/letters/a-e/ a variable 'letters' is added to the query and you can intercept it and use for your scope using pre_get_posts hook

add_action('pre_get_posts', 'glossary_range_query');

function glossary_range_query( $query ) {
  if ( ! is_admin() && $query->is_main_query() && $query->get('letters') ) {
     $letters = explode('-', $query->get('letters') );
     if ( count($letters) == 2 ) {
        $tax_query = array(
          'taxonomy' => 'glossary',
          'field' => 'slug',
          'terms' => range($letters[0], $letters[1])
        );
        $query->set('tax_query', array($tax_query) );    
        $query->set('letters', NULL );     
     }
  }
}

Now you have to visit the Settings->Permalinks page on your backend and save changes to flush rewrite rules and then you are done.

Now probably you have to create a fucntion to show the link to range pages, something like:

function get_glossary_range_url( $from = 'a', $to = 'z' ) {
  return home_url( '/letters/' . $from . '-' . $to . '/' );
}
  • Sorry, for the late replay. Been swamped. Most of all thanks so much for your reply. I haven't had time to test this yet but will this week and see if I can get this working and post the results. Thanks. – scott Nov 2 '13 at 16:58
  • Ok, well finially I have had the chance to work on this. Unfortunately I couldn't get it to work the way it was need so, the easy work around was that I just created the taxonomy in the cpt. Then gave it the range needed for each term within the admin. Add the links into the template from the term the admin chooses. Not really the way I wanted to do it but it works. – scott Jan 7 '14 at 20:17
  • I would like to add here about sorting taxonomy terms with Alphabetic Pagination plugin. It simply works with pre_get_posts and where clause hook. It's query efficient technique as well. wordpress.org/plugins/alphabetic-pagination – WordPress Mechanic May 13 '17 at 18:54

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.