Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // edit these defines (articles is just an example of a Page slug)
- define('RESULTS_PAGE_SLUG', 'articles');
- define('LETTER_PREFIX', '');
- function get_letter_ids() {
- $letter = get_query_var('letter');
- if($letter) {
- global $wpdb;
- // get post ID's that start with the letter from the query var
- return $wpdb->get_col("select ID from $wpdb->posts where $wpdb->posts.post_type = 'post' AND ($wpdb->posts.post_status = 'publish') AND post_title LIKE '".esc_sql(like_escape($letter))."%' ");
- }
- return;
- }
- function get_alphabet_links() {
- $alphabet_array = range('a','z');
- $current_letter = get_query_var('letter');
- $results_page = get_page_by_title( (string) RESULTS_PAGE_SLUG );
- $available_letters = get_available_post_letters();
- $html = '';
- if($results_page && !empty($available_letters)) {
- $page_permalink = get_permalink( $results_page );
- $html .= '<ul>';
- foreach($alphabet_array as $letter){
- if(in_array($letter, $available_letters)) {
- if($current_letter == $letter) {
- $html .= '<li class="current_letter">' . strtoupper ( $letter ) . '</li>';
- } else {
- if(get_option( 'permalink_structure' )) {
- $permalink = esc_url( $page_permalink .sanitize_title( (string) LETTER_PREFIX.$letter));
- } else {
- $permalink = add_query_arg( 'letter', $letter, esc_url($page_permalink));
- }
- $html .= '<li><a href="' . $permalink . '">' . strtoupper ( $letter ) . '</a></li>';
- }
- } else {
- $html .= '<li class="not_available_letter">' . strtoupper ( $letter ) . '</li>';
- }
- }
- $html .= '</ul>';
- }
- return $html;
- }
- function get_available_post_letters(){
- if ( false === ( $post_letters = get_transient( 'all_post_letters' ) ) ) {
- $args = array(
- 'posts_per_page' => -1,
- 'ignore_sticky_posts' => 1,
- 'post_status' => 'publish',
- 'no_found_rows' => true,
- );
- $posts = get_posts($args);
- $post_letters = array();
- if($posts) {
- foreach ($posts as $post) {
- $first_letter = strtolower(substr($post->post_title, 0, 1));
- if(preg_match('/[a-z]/', $first_letter)) {
- $post_letters[] = $first_letter;
- }
- }
- }
- $post_letters = array_unique($post_letters);
- set_transient('all_post_letters', $post_letters);
- }
- return $post_letters;
- }
- // delete transient when updating a post
- add_action( 'save_post', 'update_all_ids' );
- add_action( 'delete_post', 'update_all_ids' );
- function update_all_ids( $post_id ) {
- //verify post is not a revision
- if ( !wp_is_post_revision( $post_id ) ) {
- delete_transient( 'all_post_letters' );
- }
- }
- add_action('init', 'add_letter_rewrite_rules');
- function add_letter_rewrite_rules() {
- $prefix = trim((string) LETTER_PREFIX);
- $page_slug = trim((string) RESULTS_PAGE_SLUG);
- if(!is_admin() && get_page_by_title( $page_slug )) {
- // sanitize the letter prefix before adding the rewrite rule
- $prefix_end = '';
- if('-' == substr($prefix, -1)) {
- $prefix_end = '-';
- }
- $prefix = sanitize_title($prefix) . $prefix_end;
- $options = array(
- 'rules' => array(
- '(.?.+?)/' . $prefix . '([a-z])/page/?([0-9]{1,})/?$' => 'index.php?pagename='.$page_slug.'&letter=$matches[2]&paged=$matches[3]',
- '(.?.+?)/' . $prefix . '([a-z])(/[0-9]+)?/?$' => 'index.php?pagename='.$page_slug.'&letter=$matches[2]&page=$matches[3]',
- ),
- 'query_vars' => array('letter')
- );
- $add_rewrite_rules = new ARR_add_rewrite_rules($options);
- }
- }
- if(!class_exists('ARR_add_rewrite_rules')) {
- class ARR_add_rewrite_rules {
- var $query_vars = array();
- var $rules = array();
- function __construct($options = NULL){
- if(!is_null($options)){
- $this->init($options);
- }
- }
- function init($options){
- foreach($options as $key => $value){
- $this->$key = $value;
- }
- if(get_option( 'permalink_structure' ) && !empty($this->rules)) {
- add_action('wp_head', array(&$this, 'flush_rules'));
- add_action('generate_rewrite_rules', array(&$this, 'add_rules'));
- }
- if(!empty($this->query_vars)){
- add_filter('query_vars', array(&$this, 'add_query_vars'));
- }
- }
- function add_query_vars($query_vars){
- foreach($this->query_vars as $var){
- $query_vars[] = $var;
- }
- return $query_vars;
- }
- function add_rules(){
- global $wp_rewrite;
- $wp_rewrite->rules = $this->rules + $wp_rewrite->rules;
- }
- function rules_exist(){
- global $wp_rewrite;
- foreach($this->rules as $key => $rule){
- if(!in_array($rule, $wp_rewrite->rules) || !key_exists($key, $wp_rewrite->rules)){
- return FALSE;
- }
- }
- return TRUE;
- }
- function flush_rules(){
- global $wp_rewrite;
- // only flush rules if new rules are added
- if(!$this->rules_exist()){
- //echo 'Rules are flused (refresh the page again)';
- $wp_rewrite->flush_rules();
- }
- }
- } // class
- } // class exists
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement