3

Trying to figure out how I can output my posts based on category title (a–z) and secondly, the title of the posts within the category:

A CATEGORY
– A post beginning with a
– Because I want to be output second
– Come on, and output me already

B CATEGORY
– Another post beginning with a
– Bother, can't come up with another title on B
– I guess you get the point

How do I achieve this?

  • Did you ever follow up on the answer? Was it the solution? If not: why? – kaiser May 21 '14 at 11:25
5

To get them broken down by Category, you need to loop through the list of categories and then query on each category:

$categories = get_categories( array ('orderby' => 'name', 'order' => 'asc' ) );

foreach ($categories as $category){

   echo "Category is: $category->name <br/>";

   $catPosts = new WP_Query( array ( 'category_name' => $category->slug, 'orderby' => 'title' ) ); 

   if ( $catPosts->have_posts() ){

       while ( $catPost->have_posts() ){
          $catPost->the_post();
          echo "<a href='the_permalink()'>the_title()</a>";
       }

       echo "<p><a href='/category/$category->slug'>More in this category</a></p>";

   }//end if



} //end foreach

wp_reset_postdata();
  • 1
    For scalability, I don't recommend using multiple WP_Query items when not needed.. Using a single get_terms and a single WP_Query is all you need. Then you have all items, and can use array_filter to figure out which posts are in each category. – Eric Holmes May 23 '14 at 19:21
1

I think do you mean GROUP Post by category/taxonomy NOT SORT.

Here, Is a code to GROUP by category/taxonomy

  1. $terms = get_terms( 'my_cat_name' );

Here, cat_name name is the name of taxonomy, When you register it like this: e.g.

register_taxonomy( 'my_cat_name', array( 'custom_post_name' ), $args )
  1. Use it in Query e.g.:

    $args = array(
       'post_type'  => 'custom_post_name',
       'my_cat_name' => $term->slug,
       'posts_per_page' => $no_of_posts,
    );
    
  2. Complete code:

    $terms = get_terms( 'CUSTOM_TAXONOMY_SLUG' );
    
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    
        $output .= '<ul class="category-list">';
        foreach ( $terms as $term ) {
    
            $output .= '<li class="single-cat">';
            $output .= '    <h3>' . $term->name . '</h3>';  //  Taxonomy/Category Name
    
                $args = array(
                    'post_type'     => 'POST_TYPE_SLUG',
                    'CUSTOM_TAXONOMY_SLUG' => $term->slug,
                    'posts_per_page' => -1,
                );    
    
                $the_query = new WP_Query( $args );
    
                if ( $the_query->have_posts() ) {
                    $output .= '<ul class="cat-post-list">';
                    while ( $the_query->have_posts() ) {
                        $the_query->the_post();
                        $output .='     <li class="cat-single-post">';
                        $output .='         <h4><a href="'.get_the_permalink().'">' .get_the_title(). '</a></h4>';
                        $output .='     </li><!-- .cat-single-post -->';
                    }
                    $output .= '</ul><!-- .cat-post-list -->';
                } 
                wp_reset_postdata();
            $output .= '</li><!-- .single-cat-item -->';
        }
        $output .= '</ul><!-- .category-list -->';
    }
    
0

You can use orderby parameter for a new wp_query instance:

$query = new WP_Query( array ( 'orderby' => 'title', 'order' => 'DESC' ) );

Soo for every category use a separate instance.

More information here: http://codex.wordpress.org/Class_Reference/WP_Query

0

Expanding on Maheshwaghmare's work;

<? 
$terms = get_terms( 'CUSTOM_TAXONOMY' );
if ( ! empty( $terms ) ){ ?>

<div class="POST_TYPE_PLURAL">

<?  foreach ( $terms as $term ) {
//print_r($term) // DEBUG;
$term_slug = $term->slug;
$term_name = $term->name;
$term_description = $term->description;
?>

    <div class="POST_TYPE_CATEGORY <?=$term_slug; ?>">

      <h1 class="section-head"> <?=$term_name; ?> </h1>
      <p><?=$term_description; ?> </p>

       <?  
        $args = array(
          'post_type'     => 'CUSTOM_POST_TYPE',
          'tax_query' => array(
            array(
              'taxonomy' => 'CUSTOM_TAXONOMY',
              'field'    => 'slug',
              'terms'    => $term_slug,
            ),
          ),
           'posts_per_page' => -1,
         );    

         $the_query = new WP_Query( $args );

           if ( $the_query->have_posts() ) : ?>
              <? while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

               <div class="POST_TYPE_SINGLE">
                <h3> <? the_title(); ?> </h3>
                <? // MORE TEMPLATING CODE ?>
               </div>

             <?  endwhile;
           endif;
           wp_reset_postdata(); ?>
     </div>
 <?  } // foreach
 } //if terms
 ?>
  • I would be interested to know whether it's better practice to keep making instances of wp_query or whether it would be better to make an array from one and then use the array to populate. – Chris Pink Nov 21 '16 at 10:22
  • 2
    It would be better if you can explain the code you posted and what have you expanded from previous answer. – bravokeyl Nov 21 '16 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.