WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using these code to sort most Commented, Most viewed Posts

<?php $params = $_GET;
$url = strtok($url, '?');
$url =$url.'?'.http_build_query($params);
 <ul id="sort">
<li <?php if($active == "date"): ?> class="active"<? endif ?>><a href="http://<?php echo   
$url ?>">Most Rated</a></li>
<li <?php if($active == "comment"): ?> class="active"<? endif ?>><a  href="http://<?php   echo $url ?>&orderby=comment_count&order=desc">Most commented </a></li>
<li <?php if($active == "views"): ?> class="active"<? endif ?>><a href="http://<?php echo $url ?>&v_sortby=views&v_orderby=desc">Most viewed </a></li>

Now I want to sort Custom Field "Rating" as Most Rated here is the code of custom field.

//grab the rating value
$rating = get_post_meta($post->ID, 'rating', true);

//prints star image
for ($i=1; $i<=$rating; $i++){
    echo '<img src="/images/star.png"/>';  

//prints unstar image
for ($i=$rating; $i<10; $i++){
    echo '<img src="/images/unstar.png"/>';

For this I used this code, but it's not working.

<a href="http://<?php echo $url ?>&orderby=rating&order=desc">Most Rated</a>  
share|improve this question

From what I understood from your question, you want to sort your result by the most rated custom field 'rating' which is assumed to be number. In order to do this you need to write the below wp_query argument and to handle it properly in your request. However the end result for your query should be as follows:

$args = array(
      'meta_key' => 'rating',
      'order'   =>  'DESC',
      'orderby' => 'meta_value_num'
$query = new WP_Query($args);
share|improve this answer
Thanks for reply please have a look at alturl.com/67pjy i want to sort post via custom field value "rating" you can see there button "most-commented", most recent... – Nisha_at_Behance Aug 17 '12 at 12:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.