order alfabetical posts list – WordPress Development Stack Exchange
I’m using a script (cyb-glossary) to sort the posts alphabetically according to the first letter of the title. The script is very simple and it works. First register a taxonomy as a hidden tag, create the tag and assign it to each post. Then list the letters of the alphabet as a link for each tag. The problem that he has and that I do not understand why, is that when I click on each letter/tag it directs me to a single post and not to the tag.php template where all the posts that start with the letter of the clicked tag are listed
add_action('save_post','title_set_first_letter');
function title_set_first_letter( $post_id ){
// skip autosave
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
}
// limit to term post type
if( isset($_POST['post_type']) && 'post' != $_POST['post_type'] ) {
return;
}
// Check permissions
if( !current_user_can('edit_post', $post_id ) ) {
return;
}
//Assign first letter of the title as titulos term
if( isset($_POST['post_title']) ) {
//TODO: skip no-sense words, characters, etc
$$alphabet_term = mb_strtolower( mb_substr( $_POST['post_title'], 0, 1 ) );
wp_set_post_terms(
$post_id,
$alphabet_term,
'titles'
);
}
//delete the transient that is storing the alphabet letters
delete_transient('archive_alphabet');
}
// Set ascendent order by name in "terms" post type and titulos taxomony archives
add_action( 'pre_get_posts', function( $query ) {
if( ! $query->is_admin && $query->is_main_query() && ( $query->is_tax( 'titles' ) || $query->is_post_type_archive( 'post' ) ) ) {
$query->set( 'order', 'ASC' );
$query->set( 'orderby', 'title' );
}
} );
function title_the_alphabet_menu() {
$taxonomy ='titles';
// save the terms that have posts in an array as a transient
if( false === ( $alphabet = get_transient('archive_alphabet') ) ) {
// It wasn't there, so regenerate the data and save the transient
$terms = get_terms($taxonomy);
$alphabet = array();
if($terms){
foreach($terms as $term){
$alphabet[]= $term->slug;
}
}
set_transient('archive_alphabet', $alphabet );
}?>
<ul id="alphabet-menu">
<?php
foreach(range('a','z') as $i) {
$current = ($i == get_query_var($taxonomy)) ? "current-menu-item" : "menu-item";
if(in_array( $i, $alphabet )) {
printf('<li class="az-char %s"><a href="%s">%s</a></li>', $current, get_term_link( $i, $taxonomy ), $i );
} else {
printf('<li class="az-char %s">%s</li>', $current, $i );
}
}
?>
</ul>
<?php
}
The link appears fine but it redirects me to a single post, why?? <li class="az-char menu-item"><a href="http://localhost/library/titles/a/">a</a></li>
Leave an answer