• Hello everybody,

    I’ve been trying for a while now, to sort all my posts by category. More precisely, to have a list of all the categories, with all the relatives posts’ titles sorted by name, under the relative category.
    For that purpose I wrote this:

    <?php wp_reset_query();
    		$categories = get_categories( 'hide_empty=1' ); 
    
    		foreach($categories as $category){
    			$currentCat = get_cat_id($category);
    			echo '<div class="category"><ol>'.$category->category_nicename;
    			$myquery = new WP_Query( 'cat=$currentCat', array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );
    			while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
    					<li><a href="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    			<?php endwhile;
    			echo '</ol></div>';
    			wp_reset_postdata();
    		} ?>

    It works only partially: i get a correct alphabetical list of all the categories, but the sub-lists of posts show always the same 10 posts, the first 10 posts sorted by date.
    (LIKE THIS)
    I’ve already checked other people’s solution to similar problems, but I can’t figure out what I’m doing wrong.
    Thanks in advance…

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

The topic ‘Sort posts by category (specific problem)’ is closed to new replies.