WordPress.org

Forums

[resolved] Sort items in archive.php (8 posts)

  1. briancostea
    Member
    Posted 3 years ago #

    I am trying to allow a user to sort the posts on the archive.php file by most commented or most recent. If I change the archive.php page to include this code based a querystring parameter, it doesn't respect the month and year in the URL; it just displays all posts.

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args= array(
    'orderby' => 'comment_count',
    'order' => 'DESC',
    'paged' => $paged
    );
    query_posts($args);

    Is there a way for me to grab the month and year out of the URL so that I can display just the posts for that month and year? If so, I can add it to the arguments in the $args array.

    PS -- I am using the out of the box archive widget to get to the site.com/year/month page (which displays the archive.php from the site theme).

  2. briancostea
    Member
    Posted 3 years ago #

    Bump

  3. briancostea
    Member
    Posted 3 years ago #

    Anyone out there?

  4. esmi
    Forum Moderator
    Posted 3 years ago #

    it doesn't respect the month and year in the URL; it just displays all posts.

    That's because you have over-written the original query with your custom one. Try reviewing http://codex.wordpress.org/Function_Reference/query_posts

  5. briancostea
    Member
    Posted 3 years ago #

    Thank you for the response, esmi!

    I understand that is the issue, but I am unsure as to how best retrieve the year and month from the URL. The only way for me to do a custom sort on the page is to use a custom query :).

    I have reviewed the query_posts doc but it doesn't contain any info as to how to sort specifically inside of the archive.php or category.php files.

  6. esmi
    Forum Moderator
    Posted 3 years ago #

    The only way for me to do a custom sort on the page is to use a custom query

    That's one way to do it, yes. But instead of replacing the standard query, have you looked at concatenating your custom order & orderby parameters onto it? See Example 3 on that Codex page.

  7. briancostea
    Member
    Posted 3 years ago #

    I will try that and let you know. And thanks!

  8. briancostea
    Member
    Posted 3 years ago #

    Sorry for the late response.

    Thanks, esmi! Worked great. :)

Topic Closed

This topic has been closed to new replies.

About this Topic