How to filter posts by Letter in Wordpress only in $ _GET mode

1

Hello, I would like your help to be able to add to the code below a correction that performs alphabet filtering with letters from # to Z, only when the $_GET['letra'] parameter is executed, if it is not executed, show all the existing items, while also maintaining the current paging system operation, in both modes.

Code

 <?php
/*
Template Name: DT - Animes Legendados
*/
get_header();
doo_glossary('tvshows');
global $user_ID;
$dt = isset( $_GET['get'] ) ? $_GET['get'] : null;
$admin = isset( $_GET['admin'] ) ? $_GET['admin'] : null;
echo '<div class="module"><div class="content">';
get_template_part('inc/parts/modules/featured-post-tvshows');
echo '<div id="archive-content" class="animation-2 items calendario">';



// Ordenar em ordem alfabetica
global $wp_query;
$pages = $wp_query->max_num_pages;

$first_char = $_GET['letra'];

$postids=$wpdb->get_col($wpdb->prepare("
SELECT      ID
FROM        $wpdb->posts
WHERE       SUBSTR($wpdb->posts.post_title,1,1) = %s
ORDER BY    $wpdb->posts.post_title",$first_char)); 

query_posts(array(
    'post__in'      => $postids,
    'posts_per_page' => $pages,
    'caller_get_posts'=> 1,
    'paged' => $paged,
    'post_status'   => 'publish',
    'post_type'     => array('tvshows'),
    'meta_key'      => 'tipo_de_media',
    'meta_value'    => 'Lista',
    'order'         => 'ASC',
    'orderby'       => 'title'
));
if (have_posts()) {
echo '<header><h1>'. __d('Listagem'). '</h1><span>Total ('.$wp_query->found_posts . ')</span></header>';
    while (have_posts()) {
        the_post();
        get_template_part('inc/parts/item');
    }
}else { echo '<header><h1>'. __d('Doramas'). '</h1><span>Total ('.$wp_query->found_posts . ')</span></header>';
echo '<div class="wp-content">
<blockquote><p>No momento essa seção esta sem nenhum conteúdo, em breve ela sera ativada.</p></blockquote>
</div>'; }
echo '</div>';
if ( function_exists("pagination") ) {
    pagination();
}
echo '</div>';
get_template_part('inc/parts/sidebar');
echo '</div>';
get_footer();

Paging function

if( ! function_exists( 'pagination' ) ) {
    function pagination($pages = '', $range = 2) {
        $showitems = ($range * 2)+1;
        global $paged;
        if(empty($paged)) $paged = 1;
        if($pages == '') {
            global $wp_query;
            $pages = $wp_query->max_num_pages;
            if(!$pages) {
                $pages = 1;
            }
        }
        if(1 != $pages)  {
            echo "<div class=\"pagination\"><span>". __d('Page') ." ".$paged." " . __d('of') . " ".$pages."</span>";
            if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "";
            if($paged > 1 && $showitems < $pages) echo "<a class='arrow_pag' href='".get_pagenum_link()."'><i id='prevpagination' class='icon-caret-left'></i><i id='prevpagination' class='icon-caret-left'></i></a>";        
            if($paged > 1 && $showitems < $pages) echo "<a class='arrow_pag' href='".get_pagenum_link($paged - 1)."'><i id='prevpagination' class='icon-caret-left'></i></a>";

            for ($i=1; $i <= $pages; $i++) {
                if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
                    echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
                }
            }

            if ($paged < $pages && $showitems < $pages) echo "<a class='arrow_pag' href=\"".get_pagenum_link($paged + 1)."\"><i id='nextpagination' class='icon-caret-right'></i></a>";
            if ($paged < $pages && $showitems < $pages) echo "<a class='arrow_pag' href=\"".get_pagenum_link($pages)."\"><i id='nextpagination' class='icon-caret-right'></i><i id='nextpagination' class='icon-caret-right'></i></a>";                 
        if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "";
            echo "</div>\n";
            echo "<div class='resppages'>";
            previous_posts_link('<span class="icon-chevron-left"></span>');
            next_posts_link('<span class="icon-chevron-right"></span>');
            echo "</div>";
        }
    }
}
    
asked by anonymous 17.08.2018 / 06:52

1 answer

0

I could give you the code in hand as I did last time on the site, but it happens that the code already exists on the stack in English and even on other web sites

So I'll send you the links with them and you make the additions, okay? I hope it helps If you can not MAYBE I help with the codes

Wordpress Support (How to get initial letter posts)

StackOverflow English

Here's the fix for your updated code:

// Ordenar em ordem alfabetica
global $wp_query;
$pages = $wp_query->max_num_pages;

$first_char = 'A';

$postids=$wpdb->get_col($wpdb->prepare("
SELECT      ID
FROM        $wpdb->posts
WHERE       SUBSTR($wpdb->posts.post_title,1,1) = %s
ORDER BY    $wpdb->posts.post_title",$first_char)); 
// Agora vamos verificar se alguma letra está sendo filtrada
if ($first_char != '' ) {
query_posts(array(
    'post__in'      => $postids,
    'posts_per_page' => $pages,
    'caller_get_posts'=> 1,
    'paged' => $paged,
    'post_status'   => 'publish',
    'post_type'     => array('tvshows'),
    'meta_key'      => 'tipo_de_media',
    'meta_value'    => 'Lista',
    'order'         => 'ASC',
    'orderby'       => 'title'
));
} else {
query_posts(array(
    'posts_per_page' => $pages,
    'caller_get_posts'=> 1,
    'paged' => $paged,
    'post_status'   => 'publish',
    'post_type'     => array('tvshows'),
    'meta_key'      => 'tipo_de_media',
    'meta_value'    => 'Lista',
    'order'         => 'ASC',
    'orderby'       => 'title'
));
}
if (have_posts()) {
    
18.08.2018 / 05:09