• Hey Forum,

    I’m trying to think of a way to sort my posts numerically via contents of one of my custom fields. The site is for an auction house who want to sort posts via lot number.

    Below is the code I have so far – any help would be great:

    `<!–<?php
    query_posts(‘cat=4’);

    while (have_posts()) : the_post();
    the_title();
    the_content();
    the_meta();
    endwhile;

    ?>
    –>

    <!– Start the Loop. –>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();

    // set up variables to hold custom field entries.
    $subtitle = get_post_meta($post->ID, ‘subtitle’, true);
    $lotnum = get_post_meta($post->ID, ‘lot_number’, true);
    $description = get_post_meta($post->ID, ‘description’, true);
    $estimate = get_post_meta($post->ID, ‘estimate’, true);
    $auctiondate = get_post_meta($post->ID, ‘auction_date’, true);
    $image = get_post_meta($post->ID, ‘image’, true);?>

    <!– Display the Post’s Content in a div box. –>

    <div class=”product-box”>

      <li class=”title”>Title: <?php the_title();?>
      <li class=”subtitle”>Subtitle: <?php echo $subtitle; ?>

      <!– Display our Custom Field –>

    • Lot Number: <?php echo $lotnum; ?>
    • Description: <?php echo $description; ?>
    • Guide Price: <?php echo $estimate; ?>
    • Auction Date: <?php echo $auctiondate; ?>
    • <?php echo $image; ?>
    • <li class=”galleryset”><?php the_content();?>

    </div> <!– /product box –>
    <!– Stop The Loop (but note the “else:” – see next line). –>
    <?php endwhile; else: ?>

    <!– The very first “if” tested to see if there were any Posts to –>
    <!– display. This “else” part tells what do if there weren’t any. –>
    <p>Our next auction catalogue will be published here shortly – please visit again soon</p>

    <!– REALLY stop The Loop. –>
    <?php endif; ?>

Viewing 5 replies - 1 through 5 (of 5 total)
  • I don’t understand the purpose of this part of the code:

    <!--<?php
    query_posts('cat=4');
    
    while (have_posts()) : the_post();
    the_title();
    the_content();
    the_meta();
    endwhile;
    
    ?>
    -->

    But, assuming that the posts you want are in category 4, this should be close to what you want:

    <!-- Start the Loop. -->
    <?php
    if (!$paged = get_query_var('paged')) {
       if (!$paged = get_query_var('page')) {
          $paged = 1;
       }
    }
    $args = array(
       'cat' => 4,
       'meta_key' => 'lot_number',
       'orderby' => 'meta_value_num',
       'paged' => $paged,
    );
    query_posts($args);
    ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();
    Thread Starter shellfish

    (@shellfish)

    Hey,
    Thanks for response. Tried the code and must be doing something wrong as still doesn’t sort them in the right order. My code now looks like this:

    <!-- Start the Loop. -->
    <?php
    if (!$paged = get_query_var('paged')) {
       if (!$paged = get_query_var('page')) {
          $paged = 1;
       }
    }
    $args = array(
       'cat' => 3,
       'meta_key' => 'lot_number',
       'orderby' => 'meta_value_num',
       'paged' => $paged,
    );
    query_posts($args);
    ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();
    
    // set up variables to hold custom field entries.
    $subtitle = get_post_meta($post->ID, 'subtitle', true);
    $lotnum = get_post_meta($post->ID, 'lot_number', true);
    $description = get_post_meta($post->ID, 'description', true);
    $estimate = get_post_meta($post->ID, 'estimate', true);
     $auctiondate = get_post_meta($post->ID, 'auction_date', true);
    $image = get_post_meta($post->ID, 'image', true);
    
    ?>
    
     <!-- Display the Post's Content in a div box. -->
    
    <div class="product-box">
    <ul>
    
    <li class="title">Title: <?php the_title();?></li>
    <li class="subtitle"><em>Subtitle:</em> <?php echo $subtitle; ?></li>
    
    <!-- Display our Custom Field -->
    <li><em>Lot Number:</em> <?php echo $lotnum; ?></li>
    <li><em>Description:</em> <?php echo $description; ?></li>
    <li><em>Guide Price:</em> <?php echo $estimate; ?>  </li>
    <li><em>Auction Date:</em> <?php echo $auctiondate; ?></li>
    <li><?php echo $image; ?></li>
    <li class="galleryset"><?php the_content();?></li>
    </ul>
    </div> <!-- /product box --><br/>
     <!-- Stop The Loop (but note the "else:" - see next line). -->
     <?php endwhile; else: ?>
    
     <!-- The very first "if" tested to see if there were any Posts to -->
     <!-- display.  This "else" part tells what do if there weren't any. -->
     <p>Our next auction catalogue will be published here shortly - please visit again soon</p>

    Any ideas?
    Thanks :0)

    What version of WP are you using? meta_value_num is only available in 2.8+.

    What order are you seeing?

    Thread Starter shellfish

    (@shellfish)

    Hey,

    I’m using version 2.9.1. I’m still seeing the published order.

    Thanks

    Thread Starter shellfish

    (@shellfish)

    Ok – At last I’ve worked it out thanks to you and a bit of goggling!

    I did a combination of your code and using the wp smart sort to sort numerically by custom field [lot_number]

    In case this helps anyone else:

    <?php
    
    if (!$paged = get_query_var('paged')) {
       if (!$paged = get_query_var('page')) {
          $paged = 1;
       }
    }
    $args = array(
       'cat' => 3,
       'paged' => $paged,
    );
    query_posts($args);
    
    ?>
    
    <!-- Start the Loop. -->
    
    <?php
    
     if (have_posts()) : 
    
    ?>
    
    		<?php
    
     while (have_posts()) : the_post(); 
    
    ?>
    
    	<?php $lotnum = get_post_meta($post->ID, 'lot_number', true); ?>
    
    			<?php the_title(); ?>
    
    				<div class="entry">
    					<?php the_content(); ?>
    <li><em>Lot Number:</em> <?php echo $lotnum; ?></li>
    
    				</div>
    
    			</div>
    
    		<?php endwhile; ?>
    
    <?php if(function_exists('wp_paginate')) {
        wp_paginate();
    } ?>
    
    	<?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    		<?php get_search_form(); ?>
    
    	<?php endif; ?>

    [note – i’ve taken out the other custom fields for now]

    Thanks for your help.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Orderby custom field contents’ is closed to new replies.