• 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 6 replies - 16 through 21 (of 21 total)
  • <?php $price = get_post_meta($post->ID, 'price', true); ?>
    <?php if( $price ) echo '$' .$price; ?>

    HTH

    David

    Thread Starter francissimisim

    (@francissimisim)

    Hi David, it’s giving me a $ sign with the value “Concept” so it shows $Concept.

    How can I remove this?

    <?php $price = get_post_meta($post->ID, 'price', true); ?>
    <?php if( $price ) : ?>
       echo  is_numeric($price) ? '$' .$price : $price;
    <?php endif; ?>

    Can you post the sortorder snippit in backticks ` for your solution to help others.

    HTH

    David

    Thread Starter francissimisim

    (@francissimisim)

    YES! It worked! Thank you very much vtxyzzy and Digital Raindrops! I appreciate your help very much! Thank you Thank you!

    Guys, one last thing, is there a plugin or something that will measure views? I wanted to make a certain category that can sort by popularity which is most views.

    I don’t know if you have any prices > 999.99, but if you want them formatted with commas and cents, you could use this:

    <?php if( $price ) : ?>
       echo  is_numeric($price) ? '$' . number_format($price,2) : $price;
    <?php endif; ?>

    You should also be aware that this will not print anything if the price is 0 because the if ( $price ) test will return false. So if you have any need to print zero prices just use the echo statement by itself.

    I have used the Count Per Day plugin to count the number of visits and views, but I don’t know if you can sort by the number of views.

    Thread Starter francissimisim

    (@francissimisim)

    Vtxyzzy and Digital Raindrops.. Guys you helped me so much and I’d like to thank you both very much. I’ve resolved my issue now thanks to you guys!

Viewing 6 replies - 16 through 21 (of 21 total)

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