Support » Fixing WordPress » Show Category and Posts with loop will NOT Order by newest category

  • Resolved Snazburger

    (@snazburger)


    This code does everything I need it to do except list the categories and posts in reverse. It is listing old to new instead of new to old.

    <?php
                $catQuery = $wpdb->get_results(" SELECT * FROM $wpdb->terms AS wterms
    										     INNER JOIN $wpdb->term_taxonomy AS wtaxonomy
    										     ON ( wterms.term_id = wtaxonomy.term_id )
    										     WHERE wtaxonomy.taxonomy = 'category'
    										     AND wtaxonomy.parent = 0 AND wtaxonomy.count > 0");
    
                $catCounter = 0;
                foreach ($catQuery as $category) { //latest posts 
    
    				$catCounter++;
    				$catStyle = '';
    
    				if (is_int($catCounter / 2)) $catStyle = ' class="catAlt"';
    
    				echo '<TR HEIGHT="20" VALIGN="BOTTOM">
    				      <TD COLSPAN="2"><SPAN CLASS="style10">
    					  <div id="Category_format" style"text-align:center; font-family: Arial, Helvetica, sans-serif; font-size: 11px; color: #666666; line-height: 13px; font-weight: bold;">
    					  '.$category->name.'
    					  </div></TD></TR>'; 
    
    				query_posts('cat='.$category->term_id.'&showposts=99&orderby=category&order=desc'); //this controls the order of the posts with in categories ?>
    
    				<?php while (have_posts()) : the_post(); ?>
    
    					<tr><td align="right"><?php the_title(); ?></td><td align="left" style="line-height:50%;">
                        <span class="links"><?php the_content(); ?></span></td></tr>
    
    				<?php endwhile; ?>
    
                <?php }  ?>

    `

Viewing 2 replies - 1 through 2 (of 2 total)
  • Michael

    (@alchymyth)

    that is how i understand the code:
    – you are getting all categories (no sorting order specified)
    – then for each category you get posts in that category; (sorting: ‘orderby=category’ );

    and you want to show:
    – latest category first with its posts in descending order;
    – the the next older category with its posts;
    – etc.

    the latest category seems only be specified by the auto-incremented ‘term_taxonomy_id’ –
    you could try and use that as a sorting criterium:

    $catQuery = $wpdb->get_results(" SELECT * FROM $wpdb->terms AS wterms
    	INNER JOIN $wpdb->term_taxonomy AS wtaxonomy
    	ON ( wterms.term_id = wtaxonomy.term_id )
    	WHERE wtaxonomy.taxonomy = 'category'
    	AND wtaxonomy.parent = 0 AND wtaxonomy.count > 0
    	ORDER BY wtaxonomy.term_taxonomy_id DESC");

    and try to change to ‘orderby=date’:
    query_posts('cat='.$category->term_id.'&showposts=99&orderby=date&order=desc');

    Snazburger

    (@snazburger)

    YOU ARE THE BEST!!!!! THANK YOU THANK YOU THANK YOU!!!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Show Category and Posts with loop will NOT Order by newest category’ is closed to new replies.