Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The suggested code needs to be integrated into your theme’s template used to display the post list. The key element is where a category header is output when the current post’s category does not match the previous post’s category. I mean this part and the 3 lines after it: if($current_taxonomy != $current_tax_terms) {. You also need the parts where those variables get assigned values. For other elements, you’ll want to use the code from your theme so its styling gets properly applied.

    WP does not have a built in way to order posts by category name, so a custom SQL query would need to be created, or you could expend a lot of effort through filtering to get WP_Query to work the way you want.

    I don’t know what request you want used to get this list in return. One solution is to create a page for the purpose and utilize a custom page template to make the SQL query and display the results. You can start with your theme’s page.php or index.php template as a starting point, but in making a SQL query through the global $wpdb object, you wouldn’t use the usual while (have_posts()): loop structure. Instead just foreach through the returned results. A query through $wpdb which orders posts by category looks something like this:

    global $wpdb;
    $query = $wpdb->get_results( "SELECT * FROM $wpdb->posts AS p
    	LEFT JOIN $wpdb->term_relationships AS r ON (p.ID = r.object_id)
    	INNER JOIN $wpdb->term_taxonomy AS x ON (r.term_taxonomy_id = x.term_taxonomy_id)
    	INNER JOIN $wpdb->terms AS t ON (r.term_taxonomy_id = t.term_id)
    	WHERE p.post_type = 'post'
    	AND p.post_status = 'publish'
    	AND x.taxonomy = 'category'
    	ORDER BY t.name ASC, p.post_date DESC;"
    );
    Thread Starter Fawzi

    (@fauzurrahman15)

    atm, i use this

    <?php
    //for each category, show all posts
    $cat_args=array(
      'orderby' => 'name',
      'order' => 'ASC'
       );
    $categories=get_categories($cat_args);
      foreach($categories as $category) {
        $args=array(
          'showposts' => -1,
          'category__in' => array($category->term_id),
          'caller_get_posts'=>1
        );
        $posts=get_posts($args);
          if ($posts) {
            echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
            foreach($posts as $post) {
              setup_postdata($post); ?>
              <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
              <?php
            } // foreach($posts
          } // if ($posts
        } // foreach($categories
    ?>

    as you suggest to use foreach but it’s limited to certain amount of post because i doesn’t have pagination.

    Moderator bcworkz

    (@bcworkz)

    OK, that’s fine. It’s not very efficient because of a separate post query for each term, but it’s much easier to code for. The single post query approach used in the SE answer you linked is more efficient, but rather complicated. I tried to simplify in my reply, but it’s still more complicated than your current approach.

    foreach works for all posts or limited pagination. It cycles through all items returned in an array, whether it be 1, 10, or 157 items. It’s best when working with a simple array of posts. The WP loop using while( has_posts()): works well with the WP_Query object because the object keeps track of what posts were processed. A simple array doesn’t do that for us.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Group post by taxonomy and its pagination’ is closed to new replies.