• Resolved francissimisim

    (@francissimisim)


    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)
  • Do you want to sort by price in all categories, or just certain ones?

    Thread Starter francissimisim

    (@francissimisim)

    hi vtxyzzy, I’d like to sort by price for certain categories only.

    Thread Starter francissimisim

    (@francissimisim)

    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).

    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 );
    }

    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

    (@francissimisim)

    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

    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

    (@francissimisim)

    Hi guys, anyone that can help?

    Patience please. I have to download the theme and test the code.

    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

    (@francissimisim)

    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

    (@francissimisim)

    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?

    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

    (@francissimisim)

    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?

    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

    (@francissimisim)

    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)

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