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

Asked

Viewed 165 times

1

Hello I would like your help to be able to add to the code below, a correction that will perform alphabetic filtering with letters from # to Z, only when the parameter $_GET['letra'] is executed, if it is not executed, all existing items are shown, also keeping the current paging system working, 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();

Pagination 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>";
        }
    }
}

1 answer

0


I could give you all the code as I did the last times here on the site, but it turns out that the code already exists in the stack in English and even in other web sites

So I’ll send you the links with them and you make the additions ok? I hope it helps If you can’t PERHAPS I help with the codes

Wordpress Support (How to pick up posts with initial letter)

Stackoverflow English

Here is the correction of 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()) {
  • Thank you so much, any mistakes I can make ?

  • Thanks I got it, but now I have a problem, I would like to know how I can perform this process only via get method for the variable '$first_char', if no letter is selected as I can keep the current code working, showing all existing items.

  • Ready I updated my question, I hope you’re right.

  • Updated answer. Test there to see if it’s OK now.

  • It worked perfectly, thank you very much, for your help.

  • Thank you very much, if you are not asking a lot you would know how to answer this question https://answall.com/questions/323298/como-crea-par%C3%A2metros-de-urls-friendliest-no-wordpress ?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.