• I’m creating a theme with the option to create an online store. One option that needs to be included is a sort feature (i.e. priced low to high, high to low, a-z, etc.). The following code is what I worked up:

    <?php
    	// Let's start by checking the URL for variables
    	$page  = $_GET['sort'];
    	// Let's sort the store posts
    	if ($page == false) {}
    	elseif ($page == 'low-to-high') {
    		query_posts('meta_key=price&meta_value=&order=ASC&cat=' . $category_id);
    		}
    	elseif ($page == 'high-to-low') {
    		query_posts('meta_key=price&meta_value=&order=DESC&cat=' . $category_id);
    		}
    	elseif ($page == 'a-z') {
    		query_posts('orderby=title&meta_value=&order=ASC&cat=' . $category_id);
    		}
    	elseif ($page == 'z-a') {
    		query_posts('orderby=title&meta_value=&order=DESC&cat=' . $category_id);
    		}
    	// The Loop
    	if(have_posts()): while(have_posts()): the_post();
    	?>
    		<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    			<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php include(TEMPLATEPATH . '/thumb.php'); ?><span><?php the_title(); ?></span></a>
    			<?php if(get_post_meta($post->ID, 'Price', true)) {
    				echo '<div id="cat-price"><strong>Our Price:</strong>&nbsp;<span>' . get_post_meta($post->ID, 'Price', true) . '</span></div>';
    			} ?>
    			<?php edit_post_link( 'Edit', '<span class="the-edit">', '</span>' ); ?>
    		</div><!-- id="post-<?php the_ID(); ?>" -->
    	<?php
    	endwhile; else: endif;
    	wp_reset_query();
    	?>

    It seems to be missing something because when i try to sort using the META_KEY and META_VALUE, the order is incorrect (the order to its own child-category is correct but not on the parent category).

    I’m not sure if I’m explaining this correctly and hope someone understands and can help.

    Basicly: If I try to order the posts by the meta_key and meta_value, the output is incorrect in out of order.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You’ve got an empty meta_value when you should be ordering by meta_value. At least that’s my take on it. Try:

    if ($page == false) {}
    	elseif ($page == 'low-to-high') {
    		query_posts('meta_key=price&orderby=meta_value&order=ASC&cat=' . $category_id);
    		}
    	elseif ($page == 'high-to-low') {
    		query_posts('meta_key=price&orderby=meta_value&order=DESC&cat=' . $category_id);
    		}
    	elseif ($page == 'a-z') {
    		query_posts('orderby=title&orderby=meta_value&order=ASC&cat=' . $category_id);
    		}
    	elseif ($page == 'z-a') {
    		query_posts('orderby=title&orderby=meta_value&order=DESC&cat=' . $category_id);
    		}
    Thread Starter tenetcommunity

    (@tenetcommunity)

    thanks,
    tried it, didn’t work…it reversed the order…tried this other code but didn’t work either

    if ($page == false) {}
    	elseif ($page == 'low-to-high') {
    		query_posts('orderby=meta_value&meta_key=price&order=ASC&cat=' . $category_id);
    		}
    	elseif ($page == 'high-to-low') {
    		query_posts('orderby=meta_value&meta_key=price&order=DESC&cat=' . $category_id);
    		}
    	elseif ($page == 'a-z') {
    		query_posts('orderby=title&order=ASC&cat=' . $category_id);
    		}
    	elseif ($page == 'z-a') {
    		query_posts('orderby=title&order=DESC&cat=' . $category_id);
    		}

    i’m brain fried from working on this theme but i’ll post more info asap

    hello, here with a diff screen name…

    still no resolve with this issue…

    using orderby=meta_value&meta_key=whatever is still giving incorrect results…how can this be sorted using a small php code?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Need help sorting posts from query_posts using meta_key and meta_value’ is closed to new replies.