Title: Arrange Posts by Custom Fields
Last modified: August 20, 2016

---

# Arrange Posts by Custom Fields

 *  Resolved [francissimisim](https://wordpress.org/support/users/francissimisim/)
 * (@francissimisim)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/)
 * Hi everyone, I’d like to ask for help on arranging Posts by custom fields in 
   category.php.
 * I’m trying to sort certain categories’ posts by price which are added via custom
   field “price”. I.E. High price and Low Price.
 * This is the query that my theme does at the moment:
 *     ```
       <?php
   
               $x = 0;
   
               $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
   
               if($paged > 1) 
   
                 $y = (0 + (($paged-1) * 12));
   
               else
   
                 $y = 0;
   
       	global $wp_query;
   
       	$args = array_merge( $wp_query->query, array( 'posts_per_page' => 12 ) );
   
       	query_posts( $args );
   
               while (have_posts()) : the_post(); ?>
       ```
   
 * Thank you so much for your help guys!

Viewing 15 replies - 1 through 15 (of 21 total)

1 [2](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/page/2/?output_format=md)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/#post-2791964)
 * Do you want to sort by price in all categories, or just certain ones?
 *  Thread Starter [francissimisim](https://wordpress.org/support/users/francissimisim/)
 * (@francissimisim)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/#post-2792003)
 * hi vtxyzzy, I’d like to sort by price for certain categories only.
 *  Thread Starter [francissimisim](https://wordpress.org/support/users/francissimisim/)
 * (@francissimisim)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/#post-2792034)
 * I just read it again and sorry if it seems a bit vague. Here’s what I mean:
 * I have a custom field ‘price’ and I’d like to use this value to sort certain 
   categories, namely High-Price and Low-Price, to Ascending and Descending order
   respectively.
 * But I’d like the rest of the categories to sort by default (post date).
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/#post-2792102)
 * Please try replacing this:
 *     ```
       global $wp_query;
   
       $args = array_merge( $wp_query->query, array( 'posts_per_page' => 12 ) );
   
       query_posts( $args );
       ```
   
 * with this (replace 97 with the ID of your category):
 *     ```
       $args = array( 'posts_per_page' => 12 );
       $cats_with_price = array(97); // The IDs of categories to sort by price
       if ( in_category($cats_with_price) ) {
          $args['meta_key'] = 'price';
          $args['orderby'] = 'meta_value_num';
          $args['order'] = 'ASC';
          global $wp_query;
          $args = array_merge( $wp_query->query, $args );
          query_posts( $args );
       }
       ```
   
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/#post-2792103)
 * Now, I just reread your last post and see that you do not want to just sort on
   price, but sort two ways. That is more complicated as it will depend on your 
   theme. I will have to post that in a pastebin.
 * I can only do this if you are using a free theme. If so, please post a link to
   your site.
 * What are the IDs of ‘High-Price’ and ‘Low-Price’?
 *  Thread Starter [francissimisim](https://wordpress.org/support/users/francissimisim/)
 * (@francissimisim)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/#post-2792133)
 * Hi vtxyzzy, the ids of high-price is 361 and low-price is 362.
 * I’m using a free theme. the url for my site is [IronMuffin.com](http://ironmuffin.com)
 * What I really want to do is to be able to have the categories high-price and 
   low-price sort all my items to DSC and ASC respectively, WHILE the other categories
   would display the posts on default (which is by publish date).
 * In theory here’s what I want:
 * If high-price then sort the category DSC
 * If low-price then sort the category ASC
 * Else sort the category by publish date
 *  Thread Starter [francissimisim](https://wordpress.org/support/users/francissimisim/)
 * (@francissimisim)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/#post-2792182)
 * Hi guys, anyone that can help?
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/#post-2792183)
 * Patience please. I have to download the theme and test the code.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/#post-2792184)
 * Please replace this:
 *     ```
       $args = array_merge( $wp_query->query, array( 'posts_per_page' => 12 ) );
   
       query_posts( $args );
       ```
   
 *     ```
       $args = array_merge( $wp_query->query, array( 'posts_per_page' => 9 ) );
   
       if ( is_category('High Price') ) {
          $args['order'] = 'DESC';
          $args['meta_key'] = 'price';
          $args['orderby'] = 'meta_value_num';
       } elseif ( is_category('Low Price') ) {
          $args['order'] = 'ASC';
          $args['meta_key'] = 'price';
          $args['orderby'] = 'meta_value_num';
       }
       query_posts( $args );
       ```
   
 *  Thread Starter [francissimisim](https://wordpress.org/support/users/francissimisim/)
 * (@francissimisim)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/#post-2792186)
 * Hi vtxyzzy, thank you very much for the help. Sorry if I got impatient.
 * It’s not completely working. It just arranged low-price and high-price by date
   and both from oldest to newest. It should be working but I don’t know why it’s
   not.
 * Is it possible that because I have custom fields under price with the dollar (
   $) sign?
 *  Thread Starter [francissimisim](https://wordpress.org/support/users/francissimisim/)
 * (@francissimisim)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/#post-2792187)
 * I tried changing the orderby to meta_value and I’m now seeing sorted results 
   but it’s a bit weird.
 * It sorts $9.99 $9.47 $699 $5 $39.99 for high-price.
 * The problem I have is that my custom field price have fields like this:
 * – concept
    – $9.99 – $699
 * Does meta_value_num allow it to arrange by price?
 *  [Digital Raindrops](https://wordpress.org/support/users/adeptris/)
 * (@adeptris)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/#post-2792189)
 * Hi,
    It looks like it is sorting Alpha-Numeric so it would groups all $ nines
   together, $ sixes, $ fives, and $ threes, like: $9.99 $9.47 $9.1 $9 $699 $65 
   $6 $5 $3.99 $3.1
 * ‘meta_value_num’ will ignore entries that do not have a numeric value in the 
   meta key, not sure how this will sort with the leading $, test it with your price
   field.
 * Alternative:
    You could add another meta field to use for the sort, value * 100
   and adding a 1 + leading zeros for a fixed string length? $9.99 = 10000999 $947
   = 10094700 $699 = 10069900 $5 = 10000500 $39.99 = 10003999
 * Untested but it should then sort ok
    10094700 10069900 10003999 10000999 10000500
 * HTH
 * David
 *  Thread Starter [francissimisim](https://wordpress.org/support/users/francissimisim/)
 * (@francissimisim)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/#post-2792190)
 * Hi David, thank you for helping. The problem I have is that I use that value 
   to display on my homepage as well as on category pages. If I do add the 100’s
   then my website might look a bit weird.
 * Is there really no other way in sorting with the dollar ($) sign?
 *  [Digital Raindrops](https://wordpress.org/support/users/adeptris/)
 * (@adeptris)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/#post-2792191)
 * You can add a meta field for the sort and not display it so you have a ‘price_key’
   and a ‘price’ field
 * 1. try a double orderby sort with and without the dollar, and check the sort 
   results.
 *     ```
       $args = array(
               'meta_key' => 'price',
               'orderby' => meta_value meta_value_num,
               'order' => 'ASC'
       );
       ```
   
 * 2. Do not store the $ in the custom meta field just add it to the output display,
   something like?
 *     ```
       <!-- With a Price $ Label -->
       Price $<?php echo get_post_meta( the_ID(), 'price', true ); ?>
   
       <!-- With a Multi-Language Price $ Label -->
       <?php echo __('Price $') .get_post_meta( the_ID(), 'price', true ); ?>
   
       <!-- $ Without a Label -->
       $<?php echo get_post_meta( the_ID(), 'price', true ); ?>
       ```
   
 * HTH
 * David
 *  Thread Starter [francissimisim](https://wordpress.org/support/users/francissimisim/)
 * (@francissimisim)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/#post-2792192)
 * 1. Yes, I’ve done that. It worked out for some but not for all.
 * 2. I’m currently using :
    <?php $price = get_post_meta($post->ID, ‘price’, true);?
   >
 * <?php echo $price; ?>
 * I understand what you mean theoretically but how could I do that in code?

Viewing 15 replies - 1 through 15 (of 21 total)

1 [2](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/page/2/?output_format=md)

The topic ‘Arrange Posts by Custom Fields’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 21 replies
 * 3 participants
 * Last reply from: [francissimisim](https://wordpress.org/support/users/francissimisim/)
 * Last activity: [14 years, 2 months ago](https://wordpress.org/support/topic/arrange-posts-by-custom-fields/page/2/#post-2792214)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
