is_main_query() && isset( $wp_query->query_vars['a2zaal'] ) ) { // if we are on the main query and the query var 'a2zaal' exists, modify the where/orderby statements add_filter( 'posts_where', 'a2zaal_modify_query_where' ); add_filter( 'posts_orderby', 'a2zaal_modify_query_orderby' ); } } function a2zaal_modify_query_where( $where ) { global $wp_query, $wpdb; $where .= " AND substring( TRIM( LEADING 'A ' FROM TRIM( LEADING 'AN ' FROM TRIM( LEADING 'THE ' FROM UPPER( $wpdb->posts.post_title ) ) ) ), 1, 1) = '" . $wp_query->query_vars['a2zaal'] . "'"; return $where; } function a2zaal_modify_query_orderby( $orderby ) { global $wp_query, $wpdb; $orderby = "( TRIM( LEADING 'A ' FROM TRIM( LEADING 'AN ' FROM TRIM( LEADING 'THE ' FROM UPPER( $wpdb->posts.post_title ) ) ) ) )"; return $orderby; } function a2zaal_register_widgets() { register_widget( 'a2zaal_widget' ); } class a2zaal_widget extends WP_Widget { function a2zaal_widget() { $opts = array( 'classname' => 'a2zaal_widget', 'description' => 'Display a list of post/cpt title initials that link to a list of posts beginning with that initial.', ); $this->WP_Widget( 'a2zaal_widget', 'A2Z Alphabetical Archive Links', $opts ); } function form( $instance ) { $available_posts_types = get_post_types( array( 'publicly_queryable' => true ) ); $exclude_pts = array( 'attachment' ); $defaults = array( 'post_type' => 'post', 'title' => '', 'show_counts' => 0, ); $instance = wp_parse_args( (array) $instance, $defaults ); $post_type = $instance['post_type']; $title = $instance['title']; $show_counts = $instance['show_counts']; echo '
Title:
'; echo 'Post Type:
'; echo 'Show Counts:
'; } function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = strip_tags( $new_instance['title'] ); $instance['post_type'] = strip_tags( $new_instance['post_type'] ); $instance['show_counts'] = strip_tags( $new_instance['show_counts'] ); return $instance; } function widget( $args, $instance ) { extract( $args ); // run query to pull the initials for the specified post_type global $wpdb; $count_col = ''; if ( (bool) $instance['show_counts'] ) { $count_col = ", count( substring( TRIM( LEADING 'A ' FROM TRIM( LEADING 'AN ' FROM TRIM( LEADING 'THE ' FROM UPPER( $wpdb->posts.post_title ) ) ) ), 1, 1) ) as counts"; } $querystr = " SELECT DISTINCT substring( TRIM( LEADING 'A ' FROM TRIM( LEADING 'AN ' FROM TRIM( LEADING 'THE ' FROM UPPER( $wpdb->posts.post_title ) ) ) ), 1, 1) as initial" . $count_col . " FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = '" . $instance['post_type'] . "' GROUP BY initial ORDER BY TRIM( LEADING 'A ' FROM TRIM( LEADING 'AN ' FROM TRIM( LEADING 'THE ' FROM UPPER( $wpdb->posts.post_title ) ) ) ); "; $pt_initials = $wpdb->get_results( $querystr, ARRAY_A ); $initial_arr = array(); $base_url = get_post_type_archive_link( $instance['post_type'] ); if ( ! $base_url ) { if (get_option('show_on_front')=='page') { $base_url = esc_url( get_permalink( get_option('page_for_posts') ) ); } else { $base_url = esc_url( home_url( '/' ) ); } } foreach( $pt_initials AS $pt_rec ) { $link = add_query_arg( 'a2zaal', $pt_rec['initial'], $base_url ); if ( (bool) $instance['show_counts'] ) { $item = '