WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. andreInLA
    Member
    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.

    Details
    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();
    </blocquote>
    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:

    http://codex.wordpress.org/Category_Templates#What_Template_is_Used_to_Display_a_Particular_Category.3F

    (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:

    http://www.ifelse.co.uk/archives/2005/03/31/query_posts/
    http://www.ifelse.co.uk/archives/2005/04/08/query_posts-redux/

  3. Denis de Bernardy
    Member
    Posted 8 years ago #

    andreInLA:

    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 */");
    }
    add_filter('the_posts','redo_query');

  4. Kafkaesqui

    Posted 8 years ago #

    @Denis

    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:

    query_posts("category_name=profiles&orderby=title&order=ASC");

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

    &posts_per_page=1000

    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:

    &posts_per_page=$posts_per_page&paged=$page

  5. andreInLA
    Member
    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