WordPress.org

Ready to get started?Download WordPress

Forums

Make posts appear ASC but with custom coding. (5 posts)

  1. dominokid
    Member
    Posted 2 years ago #

    Hello guys, i'm in need of your precious help!

    I have many categories that shows posts.
    The query code is modified so that the category will only show posts that belong in the currently viewed category and NOT in its child subcategories.

    So far, the code works well.

    My question is, what do i have to add in order to show posts ascendingly? This is the code:

    <?php
    $subcategories = get_categories(array('type' => 'post', 'child_of' => $_GET['cat']));
    
    foreach ($subcategories as $i => $value) {
        $excluded_cats .= ",-".$value->cat_ID;
    }
    if (!empty($excluded_cats)) {
       $MainLoopArgs = array('cat' => $_GET['cat'] . $excluded_cats);
    }
    else {
       $MainLoopArgs = array('cat' => $_GET['cat']);
    }
    
    query_posts( $MainLoopArgs );
    
    while ( have_posts() ) : the_post(); ?>

    I thought by adding a:

    query_posts( $MainLoopArgs . '&order=ASC' );

    would work.. but it doesn't.

    Anyone can help me?

    Thanks in advance :)

    [No bumping. If it's that urgent, consider hiring someone.]

  2. vtxyzzy
    Member
    Posted 2 years ago #

    Add the order into your $MainLoopArgs like this:

    $MainLoopArgs = array('cat' => $_GET['cat'], 'order' => 'ASC');

    That should show the posts in 'oldest first' order.

  3. dominokid
    Member
    Posted 2 years ago #

    You mean something like that:

    $MainLoopArgs = array('cat' => $_GET['cat'], 'order' => 'ASC' . $excluded_cats);

    It didn't work. I think the excluded categories didn't work and the order did. The important thing is that the posts after being filtered to show up or not, should be order ASC. Now it shows all recent posts in ASC without filtering if they exist in an excluded category.

    You have anything in mind?

  4. vtxyzzy
    Member
    Posted 2 years ago #

    Not quite. Try this:

    $MainLoopArgs = array('cat' => $_GET['cat'] . $excluded_cats, 'order' => 'ASC');
  5. dominokid
    Member
    Posted 2 years ago #

    Great that worked perfectly!

    I thank u very much. I have tried every single way to do this, but the $excluded_cats was always last..

    You're the man, thanks again mate!

Topic Closed

This topic has been closed to new replies.

About this Topic