1

I am looking for plugin to sort posts by alphabetic orders.

Sort: A-Z Z-A Newest Oldest

and option to view per page like in stake exchange websites

Per Page: 20 40 60 All

if not exists, can i do this in query post(How ?) Please suggest me !

Thanks

1

Don't know about a plugin but you can do that easily with query_posts()

paste this two functions in your theme's functions.php file

function display_sort_links(){
    ?>
    <div class="sort_links">
        <ul>
            <li><a href="?P_O=az">A - Z</a></li>
            <li><a href="?P_O=za">Z - A</a></li>
        </ul>
    </div>
    <?php
}

function display_posts_numbers(){
    ?>
    <div class="number_links">
        <ul>
            <li><a href="?P_P=20">20</a></li>
            <li><a href="?P_P=40">40</a></li>
            <li><a href="?P_P=60">60</a></li>
            <li><a href="?P_P=-1">All</a></li>
        </ul>
    </div>
    <?php
}

then paste this in the archive file just above you loop (if have_posts())

global $query_string;
parse_str( $query_string, $args );
//check for posts number per page
if (isset($_GET['P_P'])){
    $args['posts_per_page'] = (int)$_GET['P_P'];
}
//check for posts order a-z or z-a
if (isset($_GET['P_O'])){
    switch ($_GET['P_O'])){
        case "az":
            $args['orderby'] = 'title';
            $args['order'] = 'ASC';
            break;
        case "za":
            $args['orderby'] = 'title';
            $args['order'] = 'DESC' ;
            break;
    }
}

query_posts( $args );

and where ever you want the links to show just call the functions from before :

//for a-z z-a sort use:
<?php display_sort_links(); ?>

//and for numbers use:
<?php display_posts_numbers(); ?>
| improve this answer | |
  • I want to give this a go. But i have many posts (10 thousand) and i was wondering if these queries could end up being slow and generate performance issues on busy sites? – Michael Rogers Apr 25 '17 at 0:30

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.