5

With this loop I am displaying single posts on an archive page. The posts are being sorted by the category 'Banks'. In addition to that, how can I display them in alphabetical order? I've tried using WP_Query, but cannot get it to work; it breaks my loop each time.

<h3>Banks & Credit Unions</h3>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); 
    if ( in_category( 'Banks' ) ) { ?>
        <li>
            <a href="<?php the_permalink() ?>">
                <img  src="<?php the_field( 'biller_logo' )?>">
                <?php the_field( 'biller_name' ) ?>
            </a>
        </li>
    <?php } 
endwhile; endif; ?>
</ul>
  • 1
    what archive page? is this the main query or a secondary query? – Milo Dec 4 '15 at 20:16
7

To display posts in descending alphabetical order add this to your args array (taken from the wp codex)

'orderby' => 'title',
'order'   => 'DESC',

To display posts in ascending alphabetical order just switch DESC to ASC.

So the whole thing would look like:

$args = array(
    'orderby' => 'title',
    'order'   => 'DESC',
);
$query = new WP_Query( $args );

WP_Query Order by Parameters

Or to use if you do not want to alter the main loop use get_posts. WP query alters the main loop by changing the variables of the global variable $wp_query. get_posts, on the other hand, simply references a new WP_Query object, and therefore does not affect or alter the main loop. It would be used in the same way, but changing $query = new WP_Query( $args ); to something like $query = get_posts( $args );.

If you would like to alter the main query before it is executed, you can hook into it using the function pre_get_posts.

  • 1
    it's not clear from the question if this is an additional query- pre_get_posts would be more appropriate in the case of a main query, though the parameters you'd set would be the same. – Milo Dec 4 '15 at 22:23
  • indeed, but just get_posts would be simpler. I have updated my answer with get_posts added. – yobddigi Dec 4 '15 at 23:09
1

Not the best code i've ever written here but if you want to create a list like:

  • A
  • America
  • B
  • Brazil
  • Bahamas

You could use..

<ul>
                            <?php 
                                query_posts(array( 
                                    'post_type' => 'franchise',
                                    'showposts' => 100,
                                    'orderby'=>'title','order'=>'ASC'
                                ) );  
                                $title_2 = 'A';
                            ?>
                            <?php while (have_posts()) : the_post(); ?> 
                                <?php
                                    $title = get_the_title();
                                    $title_1 = $title[0];
                                    if ($title_1 !== $title_2){
                                        echo '<li>&nbsp;</li><li><h3>' . $title_1 . '</h3></li><li>&nbsp;</li>';
                                        $title_2 = $title_1;
                                    }
                                ?>                              
                                    <li><a href="<?php echo get_the_permalink(); ?>"><?php echo get_the_title(); ?></a></li>
                                <?php endwhile;?>
                            </ul>

Like i said - not the best practice.. but you can tidy up & work from here :)

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.