• Resolved nickthenameless

    (@nickthenameless)


    i’m having a hard time doing something that i would think be fairly easy. i’m trying to display all my posts on my main page in ascending order, by the number of comments each has.

    i’ve gone into my theme’s index file and right before The Loop added….

    $posts = query_posts($query_string . '&orderby=comment_count&order=asc&posts_per_page=-1');

    …directly after that i start the if statement of The Loop. i’m not getting any errors, the page displays fine. the problem is that the comment_count column of the db isn’t incrementing when i add a comment. what am i doing wrong?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter nickthenameless

    (@nickthenameless)

    in hopes that this will get some sort of response from someone, i wanted to add some additional information. i got the comment_count column working now. when you post a comment it now increments. the attempt to sort posts by this count does not work however.

    so the line of code above doesn’t return an error and works if you change comment_count to title (it sorts them alphabetically). i’m thinking since there’s no error, that it works, but somewhere later gets sorted again? i don’t know totally guessing on this, would love some feedback from others ??

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    comment_count does indeed increment when you add a comment, there’s no changes you should need to make for that.

    But you cannot orderby comment_count, because nobody has ever wanted to do that ever before. So, in query.php on line 943, you’ll find this:
    $allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order');

    Add comment_count to that array and then your orderby should work.

    Thread Starter nickthenameless

    (@nickthenameless)

    thank you very much otto! i had to take another step to use your advice, but it now works.

    in that query.php file, on line 953, it adds the string “post_” to the beginning of the items in that array, because that is how they are named in the database (author == post_author). this change is done in a if statement because the only choice that does not have “post_” preceeding it in the db’s column names is “menu_order”. so in line 953 i replaced ‘menu_order’ with ‘comment_count’, since i always want to sort by comment count and don’t even know what menu order is.

    thanks again for your help. you rock!

    blogonexpo

    (@blogonexpo)

    Is there anyone else who can helpwith this – I couldnot get it to work

    EDIT – Never Mind – I fixed it

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘sort by number of comments’ is closed to new replies.