Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Using met_value_num in URL WordPress

  • SOLVED

The following code correctly grabs my posts and sorts them by price. All is well...

<?php
query_posts( 'category_name=featured&post_type=listing&order=ASC&orderby=meta_value_num&meta_key=price' );
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<p>
<?php $key='Price'; $meta = get_post_meta($post->ID, $key, TRUE); if($meta != '') { echo $meta; } ?>
</p>

<?php endwhile; endif;?>


Now, I'm trying to add a "Sort by" drop down (ascending or descending price). Ideally the drop down would just add the query to the URL like so:

example.com/?category_name=featured&post_type=listing&order=ASC&orderby=meta_value_num&meta_key=price

Those parameters return the correct posts but doesn't sort them correctly.

Here's the form:

<form action="#" method="post">

<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=""><?php echo attribute_escape(__('Sort...')); ?></option>
<option value="http://www.dpmdifference.com/?post_type=listing&order=ASC&orderby=meta_value_num&meta_key=price"><?php echo attribute_escape(__('Ascending')); ?></option>
<option value="http://www.dpmdifference.com/?post_type=listing&order=DESC&orderby=meta_value_num&meta_key=price"><?php echo attribute_escape(__('Decending')); ?></option>
</select>

</form>


Any ideas?

Answers (3)

2010-08-08

Mykyta Savchenko answers:

Put this somewhere in header:
$sort = $_GET['sort'];

here is the query:
query_posts( 'category_name=Featured&post_type=listing&order='.$sort.'&orderby=meta_value_num&meta_key=Price' );

and here is the form:


<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>

<option value=""><?php echo attribute_escape(__('Sort...')); ?></option>
<option value="/?sort=ASC"><?php echo attribute_escape(__('Ascending')); ?></option>
<option value="/?sort=DESC"><?php echo attribute_escape(__('Decending')); ?></option>
</select>

2010-08-05

Mike Schinkel answers:

Is it sorted by price as if a string value instead of a numeric value? Do you have "$" signs in your price field?


Patrick Daly comments:

The vaules are just numbers (i.e 500000 or 100). I'm adding commas and dollars signs with PHP.


Mike Schinkel comments:

Ah, I didn't understand your question at first. Try this (I just typed in and haven't tested yet so might have a typo):


$order = (!isset($_GET['order']) ? 'ASC' : (strtolower($_GET['order'][0])=='a' ? 'ASC' : 'DESC'));
query_posts( "category_name=featured&post_type=listing&order={$order}&orderby=meta_value_num&meta_key=price" );


Patrick Daly comments:

That doesn't seem to work. I also don't understand what its trying to do though (I'm a PHP novice). My problem isn't with the query on the page, but the query in the URL.


Mike Schinkel comments:

Sorry, I'm answering this while doing other things and not paying as much attention. How is your sort order being passed back? Via a posted <form>? If yes, what's your form method, GET or POST, i.e. <form method="get"> OR <form method="post">? If the latter replace $_GET with $_POST. If not, you need to give me more info like what the URL looks like that you want to present a sorted list.


Patrick Daly comments:

I should have posted this in the first place:

<form action="#" method="post">

<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=""><?php echo attribute_escape(__('Sort...')); ?></option>
<option value="http://www.example.com/?post_type=listing&order=ASC&orderby=meta_value_num&meta_key=price"><?php echo attribute_escape(__('Ascending')); ?></option>
<option value="http://www.example.com/?post_type=listing&order=DESC&orderby=meta_value_num&meta_key=price"><?php echo attribute_escape(__('Decending')); ?></option>
</select>

</form>


Mike Schinkel comments:

Partick: Then use $_POST instead of $_GET, i.e.

$order = (!isset($_POST['order']) ? 'ASC' : (strtolower($_POST['order'][0])=='a' ? 'ASC' : 'DESC'));

query_posts( "category_name=featured&post_type=listing&order={$order}&orderby=meta_value_num&meta_key=price" );


Buzu B: Yes the curly braces are technically not needed but including them is a best practice, especially for code for others to read and learn from to make it more obvious where the variable embeds are located.


Patrick Daly comments:

All put together it should look like this?

http://pastie.org/1078251


Patrick Daly comments:

Ok, actually $order shouldn't be in the option value. Otherwise, where is the order being set?

So, I've changed the form options to:

<option value="?order=ASC&orderby=meta_value_num&meta_key=price"><?php echo attribute_escape(__('Ascending')); ?></option>
<option value="?order=DESC&orderby=meta_value_num&meta_key=price"><?php echo attribute_escape(__('Decending')); ?></option>


Unfortunately, the page still isn't sorting. I'm hope I'm not just overlooking something you've already pointed out.


Mike Schinkel comments:

Try this. It's a standalone file you can put into test.php in the root of your website to test and see it work:

[[LINK href="http://wordpress.pastebin.com/w6zdeeaE"]]http://wordpress.pastebin.com/w6zdeeaE[[/LINK]]

I changed a few things up to "clean it up" including make sure people couldn't have it by giving back data and also used URL parameters that will make more sense to end users (if you don't like, just change them back.) I'd do more if it were my project like using jQuery's unobtrusive Javascript but this should be good enough to solve your problem, hopefully.


Mike Schinkel comments:

Status?


Patrick Daly comments:

Mike, thanks for your incredible help. Unfortunately I still couldn't get it to work, but Mykyta's solution below finally did work.

I'd still like to send you a couple bucks since you put in some real time. Do you have a paypal email address?

2010-08-05

Buzu B answers:

how are you grabbing the GET variables and using them on your query_posts function?


Patrick Daly comments:

I'm not doing anything custom here. I'm assuming WordPress parses the URL correctly like it does for all parameters. I'm wondering if it isn't parsing meta_value_num correctly (it's a new parameter in 3.0).


Buzu B comments:

So you are doing nothing to handle those parameters. I'm not sure, but I don't think wordpress grabs them automatically and inserts them on your query_posts function.

Modifiying a bit the code posted by Mike should work:

$order = (!isset($_GET['order']) ? 'ASC' : (strtolower($_GET['order'][0])=='a' ? 'ASC' : 'DESC'));

query_posts( "category_name=featured&post_type=listing&order=$order&orderby=meta_value_num&meta_key=price" );


You don't need the curly brackets.

The whole code should look like this:

$order = (!isset($_GET['order']) ? 'ASC' : (strtolower($_GET['order'][0])=='a' ? 'ASC' : 'DESC'));

query_posts( "category_name=featured&post_type=listing&order=$order&orderby=meta_value_num&meta_key=price" );

if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<p>
<?php $key='Price'; $meta = get_post_meta($post->ID, $key, TRUE); if($meta != '') { echo $meta; } ?>
</p>

<?php endwhile; endif;?>




Patrick Daly comments:

See my last reply to Mike about the form.


Buzu B comments:

Yes patrick, it should look like that.