WordPress.org

Ready to get started?Download WordPress

Forums

resort posts with dropdown (1 post)

  1. Doodlebee
    Member
    Posted 5 years ago #

    I'm working on a plugin for the current Plugin competition, and I've run into a bit of an issue I was hoping someone could nudge me on.

    I want to provide a dropdown - NOT dropdown navigation, but a regular <select> list of items - at the top of my category pages that would allow the end user to rearrange the posts based on the options in the dropdown.

    The plugin I'm working on is for a shopping cart - and I'm wanting to have the ability to sort the posts by price ("low to high" and "high to low") as well as alphabetically.

    The idea is that associated with each option would be a value that would be inserted into a query for the category template so that the page is reloaded with the "fix" to the query. So far, it's working as far as reloading the page, but it seems my "orderby" in the query is not passing.

    My function looks like this:

    function sort_order($type) {
      global $post, $showPrice;
      $title = get_the_title();
      $price = get_post_meta($post->ID, 'price_value', true);
      $sale = get_post_meta($post->ID, 'sale_value', true);	
    
      if($type == 'dropdown') { ?>
    
      <select name="sorter" id="cake_sorter">
        <option value="atoz">Alphabetically, A-Z</option>
        <option value="ztoa">Reverse Alphabetically, Z-A</option>
        <option value="hightolow">By Price, Highest to Lowest</option>
        <option value="lowtohigh">By Price, Lowest to Highest</option>
      </select>
    
    <?php } else if($type == 'value'){
            return $sorter;
          }
     }

    and the template file (relevant section) looks like so:

    <form id="product_sort" action="<?php echo $PHP_SELF;?>" method="get">
        <?php sort_order('dropdown'); ?>
        <input type="submit" name="submit" class="submit" value="sort" />
        </form>
    
    <?php $catid = get_query_var('cat');
          $category = get_category($catid);
          $cat = $category->cat_ID;
          $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
          $sortorder = sort_order('value');
    
          // now let's select the queries
          if($sortorder == 'atoz') {
    	$order = 'title&order=ASC';
          } else if($sortorder == 'ztoa') {
          	$order = 'title&order=DESC';
          } else if($sortorder == 'hightolow') {
          	$order = 'meta_key=price_value&order=DESC';
          } else if($sortorder == 'lowtohigh') {
          	$order = 'meta_key=price_value&order=ASC';
          }
    
    	  query_posts('cat=' . $cat . '&showposts=6&orderby=' . $order . '&paged=' . $paged);
    	  $wp_query->is_archive = true; $wp_query->is_home = false;
          if (have_posts()) : while (have_posts()) : the_post(); ?>

    If I use the "post" method (which is what I *think* I should be using), nothing whatsoever happens. If I use "get" (as it is now) I've noticed my $sorter value *does* appear in the URL of the page - which tells me that the value is being passed - but the posts are still not sorted.

    Anyone know what I'm missing? I've been looking it this for days, but I can't seem to see what's making it not work. I'd appreciate any extra eyes on this. Thanks so much!

Topic Closed

This topic has been closed to new replies.

About this Topic