Ready to get started?Download WordPress


Sort posts alphabetically or by date depending on category (5 posts)

  1. andreInLA
    Posted 8 years ago #

    The scoop
    I am using WordPress 1.5 as a simple CMS and would like to display posts from one category in alphabetical order while posts from another category display chronologically.

    I have category "profiles" which contains posts dedicated to "people." The title of each of the posts is the person's name. Ideally, the loop in category-1.php ("profiles" id is 1) will show the posts in alphabetical order.

    Still, I want to have the loop in category-2.php ("events") to display the posts in chronological order.

    Is this achievable by using the built-in tags

    while ( have_posts() ) : the_post();
    or would I need to write my own functions.

    Thank you in advance.
    -- Andre

  2. Kafkaesqui

    Posted 8 years ago #

    My suggestion: First, see the Codex on setting up templates for individual categories:


    (Edit: Need to look closer at forum posts...)

    Next, look into creating a custom Loop, using query_posts(), for the categories that will be organized alphabetically. A couple good articles for this:


  3. Denis de Bernardy
    Posted 8 years ago #


    ideally, you'd want to make a plugin that changes the wordpress loop's query. however, a critical plugin hook is missing to meet your requirements (i.e. you cannot select CASE to differentiate posts based on category), so you'll need to apply filters to the_posts:

    function trash_the_loop( $posts_where )
    return " AND 1 = 0 ";
    add_filter('posts_where', 'trash_the_loop', 1000);

    function redo_query($posts);
    global $wpdb;
    return $wpdb->get_results("/* query your way */");

  4. Kafkaesqui

    Posted 8 years ago #


    The plugin solution may be overkill, as one can query posts based on the criteria of title (or date, or other) order using individual category templates, or by testing the category one is in with in_category(). Then The Loop in "profiles" can be initiated with:


    To list all posts on the page, one need only insert an overly large number with the posts_per_page argument:


    Alternatively, pagination of posts (joined with posts_nav_link() or a 'paged links' plugin), can be handled by collecting the paged query:

    $page = get_query_var('paged');

    and the query_posts addition:


  5. andreInLA
    Posted 8 years ago #

    Here's what worked:

    added line: query_posts('orderby=title&order=ASC');
    if (have_posts()) : ?>

Topic Closed

This topic has been closed to new replies.

About this Topic