How can I display posts in alphabetical order, example: posts containing A, B, C

Asked

Viewed 158 times

-1

<?php
   global $wpdb;
   $first_char = 'A';
   $postids = $wpdb->get_col($wpdb->prepare("
     SELECT      ID
     FROM        $wpdb->posts
     WHERE       SUBSTR($wpdb->posts.post_title,1,1) = %s
     AND        $wpdb->posts.post_type = 'product'
     ORDER BY    $wpdb->posts.post_title", $first_char));

    if ($postids) {
      $args = array(
        'post__in' => $postids,
        'post_type' => 'product',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts' => 1
     );

$my_query = null;
$my_query = new WP_Query($args);
if ($my_query->have_posts()) {
    echo '<p>List of Posts Titles beginning with the letter <strong>' . 
    $first_char . '<strong></p>';
    $counter = 1;
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p>
            <span><?php echo $counter++; ?></span>
            <a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
        </p>

You’re showing off like this:

inserir a descrição da imagem aqui

  • In SQL, simply change from = 'A' for in ('A', 'B', ..., 'E'), nay?

  • @Andersoncarloswoss made a mistake, can make a more concrete example ?

  • @Andersoncarloswoss can help me ?

  • In the WHERE SUBSTR() he’s checking if the first character is A only. You could get all the database records and handle filtering in PHP. I believe that there will not be a supermassive number of posts to the point of locking the server.

  • @Cypherpotato can make me an example?

  • @Johnny removes the line from the where and when iterating the search result, check whether the first letter is a, b.

  • @Cypherpotato I did this but it didn’t work, it shows nothing when I change the Where line.

  • After all you want to simply sort alphabetically or also filter by the initial letter? As on your page you have List of Posts Titles beginning with the letter A, I imagine you want to make a page for each letter, right? Or would it serve to have all the posts sorted and presented on the same page? And paging, is or is not to have?

Show 3 more comments

1 answer

-1

In the example below I order a Custom Post Type listing by date:

$args = array(
            'author' => $user, //exibe apenas posts criados pelo usuário 
            'post_status' => 'publish', //pega apenas posts publicados
            'post_type' => 'me_dailyrecords', //tipo do post que deve retornar
            'paged' => $paged, //respeitando a paginação                
            'orderby' => 'meta_value_num', //para ordenar valores numéricos
            'meta_key' => $prefix . 'date', //campo data em timestamp
            'order' => 'DESC', //ordena do maior para o menor
        );

And as a result I get something like this:

Registros ordenados pela data de maneira descendente

In your case where the intention is to sort by name, you need to change in the parameter "orderby" the value of "meta_value_num" (for sorting numerical values) to the value "meta_value" which is suitable for sorting texts..

In "meta_key" you should put the name of the field you want to sort - which in your case I believe is the field "title" (default Wordpress) where is saved the title of the post.

In the parameter "order" you must change the value of the parameter to be able to order from the smallest to the largest (a,b,c), because in the block of code of the example I gave above the result is being ordered from the largest to the smallest (c,b,a) - to make this change just replace "DESC" by "ASC".

So your code would look like something:

$args = array(
    'post__in' => $postids,
    'post_type' => 'product',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'caller_get_posts' => 1,
    'orderby' => 'meta_value', //para ordenar valores NÃO numéricos
    'meta_key' => 'title', //campo com o nome/título do post (products)
    'order' => 'ASC', //instrução para ordenar de forma ascendente
 );

Note that I have only added the 3 parameters I explained just above in your $args array and with that the result of sorting should now be what you expect.

Test and tell me if it worked...

Hug!

Reference: Documentation about Wp_query present on the website of Wordpress developers

Browser other questions tagged

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