How can i get a wordpress plugin to enable visitors from frontend to sort posts by ascending order or descending order

Viewed 241

Any help will be much appreciated. I would prefer a wordpress plugin. But if there is none, I don't mind having a php code to help me. Bear in mind please, I am no php expert.

I saw this on the wordpress codex but do not know how to use it:

    function wp_list_sort( $list, $orderby = array(), $order = 'ASC', $preserve_keys = false ) {
    if ( ! is_array( $list ) ) {
        return array();
    }
 
    $util = new WP_List_Util( $list );
    return $util->sort( $orderby, $order, $preserve_keys );
}
2 Answers

You can try with this plugin to sorting your Posts and Post Types

It's a powerful plugin, Order Posts and Post Types Objects using a Drag and Drop Sortable JavaScript capability.

basically, you can order from your dashboard by this plugin but you can make it for the frontend as well but you need to do a few custom codes.

Here is another solution you may try it on your frontend code :

<div class="order-posts">
<div class="order-posts-container">
<?php
    global $wp;
    $current_url = home_url( $wp->request ) . '/'; // last part adds the "/" at the end
?>
<form name="George">
<select class="select-form" name="example" size="1" onChange="go()">
<option selected disabled class="select-form-item 0" value>-- Change order --</option>
<option class="select-form-item 1" value="<?php echo $current_url ?>">Order by default</option>
<option class="select-form-item 2" value="<?php echo $current_url ?>?orderby=title&order=asc">Order from A to Z</option>
<option class="select-form-item 3" value="<?php echo $current_url ?>?orderby=title&order=desc">Order from Z to A</option>
<option class="select-form-item 4" value="<?php echo $current_url ?>?orderby=date&order=desc">Order from new to old</option>
<option class="select-form-item 5" value="<?php echo $current_url ?>?orderby=date&order=asc">Order from old to new</option>
<option class="select-form-item 6" value="<?php echo $current_url ?>?orderby=comment_count&order=desc">Order by comment count 0 - 999</option>
<option class="select-form-item 7" value="<?php echo $current_url ?>?orderby=comment_count&order=asc">Order by comment count 999 - 0</option>
<option class="select-form-item 8" value="<?php echo $current_url ?>?orderby=rand&posts_per_page=10">Random order</option>
</select>
<script type="text/javascript">

function go(){
location=
document.George.example.
options[document.George.example.selectedIndex].value
}
 
</script>
<input type="button" class="button-form" name="sort-posts-button" value="Go!" onClick="go()">
</form>
</div>
</div>
<div style="clear:both"></div>
Related