September 08, 2021
Help visitors sort content on your website by adding filtering options to your Posts.
WP Popular Posts Plugin plugin used in the below example in order to add support for filtering Most Popular items (determined by number of views).
Place in the home.php template or whatever template being used for the Posts or Custom Post Type feed.
HTML Filters
<p>SORT BY:</p>
<ul>
<li><a href="<?php echo get_permalink(18); ?>?dir=DESC">Newest to Oldest</a></li>
<li><a href="<?php echo get_permalink(18); ?>?dir=ASC">Oldest to Newest</a></li>
<li><a href="<?php echo get_permalink(18); ?>?dir=VIEWS">Most Popular</a></li>
</ul>
PHP args to support filtering
$args = array(
'order' => (isset($_GET['dir']) ? $_GET['dir'] : 'ASC')
);
query_posts( $args );