Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 date order initially/ASC (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_post:

<?php if (have_posts()) : ?>
 <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("meta_key=property_price&orderby=meta_value_num&order=ASC&paged=$paged"); ?>
    <?php 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.

share|improve this question
add comment

2 Answers

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 achieved a page that displays on the posts we want.

If you install the Debug Bar Plugin (http://wordpress.org/extend/plugins/debug-bar/) you can verify that the category-template.php is used for this query.

2. Modify the main query via the pre_get_posts hook

If you use query_posts(), this runs an additional query to the main query, which is not only a waste of ressources, 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 this main query on a specific page to sort by the order that we want.

function my_prefix_sort_property_category( $query ) {
    if ( ! is_admin() && $query->is_main_query() && is_category( 'Property' ) ) {
        $query->set( 'meta_key', 'property_price' );
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'my_prefix_sort_property_category' );
share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.