I’ve read umpteen answers and questions about things that are very similar to this but I just can’t get it to work for my specific situation.
I have some posts with a category of “property”. In category.php
I want these posts to appear in ascending date order initially (as per usual).
I then want to have a dropdown menu, which gives the option of viewing:
- price high to low
- price low to high
- latest (default).
Each post has been assigned a custom field of “property_price” which is a numeric value.
I also need pagination to work.
Initial goes with this as my query_posts
:
<?php
if ( have_posts() ) :
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'meta_key' => 'property_price',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'paged' => $paged
);
query_posts( $args );
while ( have_posts() ) :
the_post();
This just renders a list obviously. However, even at this point, I am stuck as WordPress insists on ordering things like this: 1, 10, 100, 2, 3 so not actually in proper order. I thought using 'orderby' => 'meta_value_num'
was supposed to sort this out?
If anyone can help I’d be very grateful. Be gentle on me, I’m no expert so fool-proof instructions gladly received.
Thanks in advance.
'meta_value_num'
is not magic, it casts a value as numeric, however, to be properly cast as numeric, a value must be numeric compatible.In OP it’s said that the field values are “numeric” but I bet there’s something not numeric, e.g. a currency symbol like “10 $” or “$ 10.00”, or other symbol like “10-“.
Also consider that using a thousands separator most probably makes the casting fail, because MySQL can mistake it as a decimal separator.
When you want to use a meta field for order, follow these rules:
If you follow previous rules, the order by
'meta_value_num'
will work as expected.If you are asking yourself how to properly format a price entered using previous rules, the answer is
number_format
.As example, in Italy, a properly formatted price is ⬠1.000,50 (one thousand euros and fifty cents).
Following previous rules, one should enter that price as 1000.50
To output the proper formatted price it’s possible to use a custom function like this:
And use it like so:
That said, I strongly suggest you avoid the usage of
query_posts
and replace it with a function hooked into'pre_get_posts'
.From Codex:
So, remove any code from your
category.php
and in yourfunctions.php
write:Now, assuming that your ‘property’ archive URL is
it will, of course, show posts in category ‘property’ ordered by date (newer to older).
Using the code posted above, the URL
will show posts in category ‘property’ ascending, ordered by price, and the URL
will show posts in category ‘property’ descending, ordered by price.
So, what you need to do, is to make a dropdown that redirects the page to the proper URLs.
You can write a custom function to output the dropdown:
This function outputs a dropdown menu that allow users to choose from the 3 ordering options, and when one is selected, the page is redirected accordingly.
After having added this function in
functions.php
, put it everywhere you want in yourcategory.php
and the select will be shown.
Note that the
order_properties_menu()
function requires jQuery.You are doing a few things in a not so ideal way. So here is how to solve your problem.
1. Create a category-property.php template file
Paste a standard WordPress loop in this file. If you visit the Property category in your site, you should only see the posts filed in this category, sorted by date (newest to oldest).
By using the Template Hierarchy (http://codex.wordpress.org/Template_Hierarchy), we now have a page that displays the posts we want.
If you install the Debug Bar Plugin (http://wordpress.org/extend/plugins/debug-bar/) you can verify that
category-property.php
is used for this query.2. Modify the main query via the
pre_get_posts
filterIf you use
query_posts()
, this runs an additional query to the main query, which is not only a waste of resources, but also results in plenty of problems with pagination not working etc..So what we’ll do is write a function (that needs to be in
functions.php
) that alters the main query on a specific page to sort by the order that we want.i had this problem once, i believe you custom fiel is not set as INT right? for meta_value_num work it should be in INT, not in string, even that you write only numbers in it