0

I need to order my posts by alphabetical for each letter. I mean:

When people enter to www.xxx.com/a, they will just see posts by starting A.

ABC XYZ

ADE KLM

ARE UYZ

When people enter to www.xxx.com/b, they will just see posts by starting B.

BCK OIO

BHJ PEP

I hope you will understand what I want. I have founded this code:

    <?php
$last_char = '';
$args=array(
  'orderby' => 'title',
  'order' => 'ASC',
  'posts_per_page'=>-1,
  'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  echo 'Alphabetic index of all ' . count($my_query->posts) . ' posts';
  while ($my_query->have_posts()) : $my_query->the_post();
    $this_char = strtoupper(substr($post->post_title,0,1));
    if ($this_char != $last_char) {
      $last_char = $this_char;
      echo '<h2>'.$last_char.'</h2>';
    } ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    <?php
  endwhile;
} //if ($my_query)
wp_reset_query();  // Restore global post data stomped by the_post().
?>

However, this code lists all posts by alphabetical in one page. As I said before, I just want same thing but for one letter which I specify.

Thank you for helping me.

  • What have you tried yourself so far to achieve this? What went wrong? If you don't post your own efforts, it's quite unlikely that anyone will come to your aid :-). – engelen Jun 8 '14 at 11:22
  • Infact, I do not know anything about coding. I have tried to implement other codes which I found from some-websites. However, unfortunately it did not work. – user51315 Jun 8 '14 at 11:24
  • In a way you are just not asking the right question, do a research on the use of the Rewrite API. Another possibility is using a taxonomy. Additionally there are some good answers on alphabetic indexing already available, for example: here or here or here. – Nicolai Jun 8 '14 at 11:53
  • I have looked all of them what you shared. However, they do not work or I am not able to run them. >) I find what I need. After 8 hours, I will share it. For now, the system does not let me to share. – user51315 Jun 8 '14 at 12:00
0

This is what I find for this question. Two codes I found were coded by MichaelH from wordpress.org. Even he won't probably see this post, I want to thank him.

Now, I just need to sort these posts from specified one category. If anyone knows how to sort them with specified category, please help me. Thanks.

   <?php
    //get all post IDs for posts beginning with cap B, in title order,
    //display posts
    $first_char = 'B';

    $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)); 

    if ($postids) {
    $args=array(
      'post__in' => $postids,
      'post_type' => 'post',
      '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 'List of Posts Titles beginning with the letter '. $first_char;
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    }
    ?>
  • It is basically the same idea as my answer, except that you are hardcoding the letter :) I just want to point out, caller_get_posts is depreciated, it was replaced with ignore_sticky_posts – Pieter Goosen Jun 9 '14 at 4:16
0

This code uses the last letter in the URL to search for all posts that starts with that specific letter. So mysite.com/a will return all posts starting with the letter "a". For this to work properly, I would suggest creating a category for each letter of the alphabet.

Here is a combination several pieces of code.

Firstly, you need to get the current URL of the page you are on. Thanks to Stephen Harris, the following code will just do that

$current_url = home_url(add_query_arg(array(),$wp->request));

Secondly, now that you have the current URL, you need to strip away everything before the last/ and just keep the letter. Thanks to DisgruntledGoat

$id = substr( $current_url, strrpos( $current_url, '/' )+1 );

Ok, now that you have the last letter, you need to return that back into a query to get all the posts that start with the letter. Again, thanks go to 21zna9

$request = $id; 
    $results = $wpdb->get_results(
        "
            SELECT * FROM $wpdb->posts
            WHERE post_title LIKE '$request%'
            AND post_type = 'post'
            AND post_status = 'publish'; 
        "
    ); 

Now, putting everything into one working query.

<ul class="posts">
<?php 
    global $wpdb, $wp; 
    $current_url = home_url(add_query_arg(array(),$wp->request));
    $id = substr( $current_url, strrpos( $current_url, '/' )+1 );
    $request = $id; 
    $results = $wpdb->get_results(
        "
            SELECT * FROM $wpdb->posts
            WHERE post_title LIKE '$request%'
            AND post_type = 'post'
            AND post_status = 'publish'; 
        "
    ); 

    if ( $results ) {
        foreach ( $results as $post ) {
            setup_postdata ( $post ); ?> 

                <li>
                    <?php get_template_part( 'content', get_post_format() ); ?>
                </li>

            <?php 
        }
    } else { ?>

    <div class="alert">No posts found for this particular letter.</div>

<?php } ?>
</ul>

You will just need to modify the output of the loop to suite your needs. The code above will give you a general idea of what to do

  • Mr. Goosen, thank you for helping me. I tried this code but I think I did some part wrong because it says "No posts found for this particular letter." I find another code which I exactly want. Thanks again, have a nice day! – user51315 Jun 8 '14 at 13:54
  • Please add the code that you found as an answer. Would be great to see what you came up with. Also, it will keep your question constructive, and you might score yourself some reputation point on your answer – Pieter Goosen Jun 8 '14 at 14:03
  • 1
    I will add it but the system does not let me for now. :-) – user51315 Jun 8 '14 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.