Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Ordering items on Category pages by a custom field WordPress

  • SOLVED

Is it possible to display posts on Category pages in the order of a custom field (that's a number)?

Here are the specifics:
- The field would be called Rankings and would be an integer from 1 to 100.
- When the integer is present in a post, I would want (a) the Category page to be displayed in the order of rankings (that is, starting at the lowest number and going to the highest number for that category), and (b) the integer to be displayed on the Category page, alongside the excerpt for the post.

The easiest way to visualize this is to view these two pages (from a Drupal site that I want to streamline and convert to WP):

http://www.web100.com/photo-100/all-100: This shows what could essentially be a category (Photo 100) with posts from 1-100.

http://www.web100.com/photo-100/image-editors: This shows a subcategory, with the same sort of display (by ranking).

I am not looking for an answer with all of the code for this (though some code might be nice), but for an answer that explains the techniques/mechanism to accomplish this (and maybe some reference URLs).

Also, I would be interested in any thoughts on pricing to add this sort of functionality to a WP website.

Answers (1)

2011-04-07

AdamGold answers:

query_posts('meta_key=yourfieldname&orderby=meta_value_num');


AdamGold comments:

If you want this to be on a specific category, create a new file named category-{slug}.php
(replace the slug with your category's slug), copy the code from your category.php file if exists, if not from archive.php and add my above code in the top of the file.


Allan Hoffman comments:

Thanks, Adam!

One thing: I'm not a programmer (or even a themer), and so any code needs some bit of explanation for me (even very brief). Would that bit of code be used to order the category pages?

Also, what about the other issue that I raised -- that I would need to display the rankings (the numbers) alongside the excerpts on those category pages? I'm wondering if I would be able to achieve a look similar to this:
http://www.web100.com/photo-100/image-editors


AdamGold comments:

Okay, about the first question. To achieve the order you would like, you would need to get the posts from the database ordered by their rankings. The block of code I gave you is just what I described - get the posts from the database ordered by their custom field value (must be an integer). So your final code should be something like this (design it as you like):

query_posts('meta_key=yourfieldname&orderby=meta_value_num');

if ( have_posts() ) : while ( have_posts() ) : the_post();

?>

<div class="post">

<!-- Display the Title as a link to the Post's permalink. -->
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>

<div class="entry">
<?php the_excerpt(); ?>
</div>
<p class="postmetadata">Posted in <?php the_category(', '); ?></p>
</div> <!-- closes the first div box -->
<?php

endwhile; endif;


To get the ratings to be displayed near the excerpt, you will need to get the custom field value:
echo get_post_meta($post->ID, 'yourfieldname', true);

So the final code:
query_posts('meta_key=yourfieldname&orderby=meta_value_num');

if ( have_posts() ) : while ( have_posts() ) : the_post();

?>

<div class="post">

<!-- Display the Title as a link to the Post's permalink. -->
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>

<div class="rating">
<?php echo get_post_meta($post->ID, 'yourfieldname', true); ?>
</div>
<div class="entry">
<?php the_excerpt(); ?>
</div>
<p class="postmetadata">Posted in <?php the_category(', '); ?></p>
</div> <!-- closes the first div box -->
<?php

endwhile; endif;


Don't forget to change "yourfieldname" to your actual custom field name.