I have one question. I need to query posts from category based on the filter most favorited ( http://pastie.org/2751114 ) which is placed in custom_sort.php to my index.php page

This is how the posts from categories are called ( http://pastie.org/2751131 ) in index.php.

Is there any way to get the posts from category based on the most favorited filter?

Thank You so very much in advance :D

1 Answer 1


query_posts accepts custom query variable as arguments. So assuming that adding


to an url alters the sort order to sort by the most favourites (i.e. you've set sort_by as a recognised WordPress query variable, and setting it sorts the returned posts accordingly) then, try:

$args = array(
    'cat' => $cat_lists[$i],
    'showposts' => intval(get_theme_option_by('bn_list_per_item', 6 )),
    'sort_by' => 'most_favourites',
    'post_type' => 'post');

That should return the set number of posts from the category selected, in order of 'most_favourites'.


To alter the the value of sort_by for category with ID 30 (say), put the following, after $arg and before query_posts:

  • HAHAH it did the trick, but it's not most_favourites, instead is: most_favorites . One letter to much. Thank You so very much. You are an inspiration and a big help. I really appreciate it!
    – jimilesku
    Oct 24, 2011 at 17:16
  • is it possible to give the option for one of 3 categories to show different sort? I would like to create a most_commented filter on 1 category. Is it possible?
    – jimilesku
    Oct 24, 2011 at 17:19
  • 1
    That's because I was writing in English :P. Glad to help :D. As for using a different sorting method, you can just use an if statement to change the $args array when $cat_lists[$i] == some ID. You just need to alter the sort_by line to: 'sort_by'=> 'most_commented' Oct 24, 2011 at 17:36
  • I'm a bit of a noob so could You tell me if the IF statement comes after the query or inside the $args? could you give me an example for category number 30 in pastie maybe so I can understand and use the code?
    – jimilesku
    Oct 24, 2011 at 18:04
  • 1
    Updated my answer to include the IF statement Oct 24, 2011 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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