• Resolved operajoe

    (@operajoe)


    I was wondering how can I add a filtering and sorting option for visitors to filter and sort posts by category or custom field?

Viewing 1 replies (of 1 total)
  • you can add this to give your visitors a sorting menu my title or date

    <?php $sort= $_GET[‘sort’];
    if($sort == “title”)
    {
    $order= “orderby=title”;
    }
    if($sort == “date”)
    {
    $order= “orderby=date”;
    }
    ?>

    You can use link:
    href=”?sort=title” Sort by title
    href=”?sort=date” Sort by Date

    Or you can use a form:
    <form action=”” method=”get”>
    <select name=”sort” id=”sorting”>
    <option value=”title” <?php if ($sort == “title”){ echo ‘selected=”selected”‘; } ?> >Sort by title</option>
    <option value=”date” <?php if ($sort == “date”){ echo ‘selected=”selected”‘; }?> >Sort by publication date</option>
    </select>
    <input type=”submit” value=”Submit” />
    </form>

    <?php $loop = new WP_Query(‘cat=5&showposts=-1&’.$order.’&order=DEC’); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    ….
    <?php endwhile; ?>

Viewing 1 replies (of 1 total)
  • The topic ‘website visitors sorting and filtering posts by category or custom field’ is closed to new replies.