• I am working on customizing a category page for a friend that allows the user to sort the category by title, date, author, etc.

    My original code was:

    <?php $sort= $_GET['sort'];
    if($sort == "title")
    {
    $order= "orderby=title";
    }
    if($sort == "author")
    {
    $order= "orderby=author";
    }
    if($sort == "date")
    {
    $order= "orderby=date";
    }
    if($sort == "datemodified")
    {
    $order= "orderby=modified";
    }
    ?>
    <?php get_header(); ?>
    <!-- section id="content" role="main" -->
    <header class="header">
    <h1 class="entry-title"><?php _e( '<!-- Category Archives: -->', 'blankslate' ); ?><?php single_cat_title(); ?></h1>
    <?php if ( '' != category_description() ) echo apply_filters( 'archive_meta', '<div class="archive-meta">' . category_description() . '</div>' ); ?>
    </header>
    <div id='page-content'>
    
    <?php
    if (function_exists('wpsp_orderby_posts_form')):
      wpsp_orderby_posts_form();
    endif;
    ?>
    
    <h3>Crystal Test</h3>
    <a href="?sort=title">title</a> | <a href="?sort=author">author</a> | <a href="?sort=date">date<a> | <a href="?sort=datemodified">last modified</a>
    <?php query_posts($order.'&order=ASC&cat=9');  ?>
    <hr /><br />
    
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php get_template_part( 'entry' ); ?>
    <?php endwhile; endif; wp_reset_query();?>
    <?php get_template_part( 'nav', 'below' ); ?>
    
    <!-- /section -->
    
    <!-- ?php get_sidebar(); ? -->
    </div>
    <?php get_footer(); ?>

    and then I received feedback that I had to use wp_query instead, so I tried this instead:

    <?php $sort= $_GET['sort'];
    if($sort == "title")
    {
    $order= "orderby=title";
    }
    if($sort == "author")
    {
    $order= "orderby=author";
    }
    if($sort == "date")
    {
    $order= "orderby=date";
    }
    if($sort == "datemodified")
    {
    $order= "orderby=modified";
    }
    ?>
    <?php get_header(); ?>
    <!-- section id="content" role="main" -->
    <header class="header">
    <h1 class="entry-title"><?php _e( '<!-- Category Archives: -->', 'blankslate' ); ?><?php single_cat_title(); ?></h1>
    <?php if ( '' != category_description() ) echo apply_filters( 'archive_meta', '<div class="archive-meta">' . category_description() . '</div>' ); ?>
    </header>
    
    <div id='page-content'>
    
    <?php
    if (function_exists('wpsp_orderby_posts_form')):
      wpsp_orderby_posts_form();
    endif;
    ?>
    
    <h3>Crystal Test</h3>
    <a href="?sort=title">title</a> | <a href="?sort=author">author</a> | <a href="?sort=date">date<a> | <a href="?sort=datemodified">last modified</a>
    <hr /><br />
    <?php
    
       $args = array('cat' => 9, 'posts_per_page' => 10);
       $category_posts = new WP_Query($args);
    
       if($category_posts->have_posts()) :
          while($category_posts->have_posts()) :
             $category_posts->the_post();
    ?>
    
             <h1><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h1>  
    
    <?php
          endwhile;
       else:
    ?>
    
          Oops, there are no posts.
    
    <?php
       endif;
    ?>
    
    <!-- /section -->
    
    <!-- ?php get_sidebar(); ? -->
    </div>
    <?php get_footer(); ?>

    But sorting still does not work, and I don’t see many other people attempting this so very little has been found online on how to resolve it. I’m still poking away at it and seeing if my tired brain is missing something completely. The current live version is here for testing:

    http://midgard-media.com/odroerir/category/bookshelf/online-academic-library/

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘WP_Query and User Sorting Categories’ is closed to new replies.