Loop out custom posts with categories?

I need help. I have a bunch of categories with a couple of posts attached to each. I would want to loop them out with the category name in one loop, is this possible?

I would want somethig like this:

CATEGORY 1
-post
-post
-post
CATEGORY 2
-post
-post
-post
CATEGORY 3

etc etc

And the moment I’m just using three different loops to output the posts under each category. But that isnt a good solution if the user wants to change/add categories etc.

Can anyone help me?

One of my loops looks like this atm:

		<?php

			$args = array(

				'post_type' => 'questions',
				'taxonomy' => 'questionscat2'

			); 

			$the_query = new WP_Query( $args );

		?>

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

Not only does I have to add new loops when adding new categories, I also have to add static headings like “category4” etc.

Are you using custom post types and custom taxonomies for all your posts that you want to output?

Yes for all the posts I want to loop out on this page yes. They are all in the same custom post type (questions), but with different categories. It is a FAQ page with questions and categories.

I don’t have time to test this, but try something like this:

<?php $categories = get_categories( array('taxonomy' => 'questionscat2') ); ?>

    <?php foreach ( $categories as $category ) : ?>
    
        // Put Category name here
        // Use WP_query() to pull in all posts in this category
    
    <?php endforeach; ?>

You might have to tweak the code for finding all the categories, because custom taxonomies for custom post types behave differently from the regular posts and categories.

If I have more time later today, I will fill out the code a bit more for you, if you are still stuck.

Hi and thanks fot the answer.

I am a beginner at this but doesnt that code just output one category with the name “questionscat2” ? I havent tried it yet so maybe I’m wrong=)

What I want to do is output all posts in the posttype, ordered by category(taxonomy) AND outputting the name of the category above the first post in that category… if this is possible. This so the user can add and remove categories without me having to change anything in the template.

$categories is an array of all the categories, so the above code loops through all the categories, and then inside this loop, you can put the code to display the category name and all its posts, so for each category you will get the category name and posts associated with the category.

1 Like

Okey cool I will try it out then and let you know how it went=)

But why do we have ‘questionscat2’ in the array? (just want to understand the code so i can learn from it) =)

That was the name of the custom taxonomy in your opening post.

hi again, So now I am trying out your code snippet, but since I am a beginner at this I can’t seem to figure out what should be inside the foreach. My current code is below, which is far from correct I suppose.

<?php $categories = get_categories( array('taxonomy' => 'faq-category') ); ?>

    <?php foreach ( $categories as $category ) : ?>

		<div class="row">
			<div class="large-12 columns">
				<h2 class="heading">Here I want the category name</h2>
			</div>
			<div class="large-12 columns"> <!-- for every category I want the questions inside this element -->

				<?php

					$args = array(

						'post_type' => 'questions'

					); 

					$the_query = new WP_Query( $args );

				?>

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

				<div class="question">
					<a href="#"><span class="question"><?php the_field('question'); ?></span><span class="plus">+</span></a>
					<p class="answer"><?php the_field('answer'); ?></p>			
				</div>

				<?php endwhile; endif; wp_reset_query(); ?>

			</div>
		</div>

	<?php endforeach; ?>

$categories is an array of objects called $category. Below is a list of the properties in the category object (this is just an example - you can see what yours is like by temporarily replacing everything in the foreach loop with the statement print_r($category); ).

stdClass Object
(
[term_id] => 85
[name] => Category Name
[slug] => category-name
[term_group] => 0
[term_taxonomy_id] => 85
[taxonomy] => category
[description] =>
[parent] => 70
[count] => 0
[cat_ID] => 85
[category_count] => 0
[category_description] =>
[cat_name] => Category Name
[category_nicename] => category-name
[category_parent] => 70
)

So try putting $category->name in here: <h2 class="heading">Here I want the category name</h2>

and put the category id in the arguments list of the WP_query:

$args = array(

	'post_type' => 'questions',
   'cat' => $category->cat_ID
	); 

Disclaimer: I have not had time to test this.

1 Like

Thanks for the help dude, appreciate it.

I replaced the “here i want the category name” with <?php $category->name ?> and added the ‘cat’ => $category->cat_ID to the args array, but now nothing is showing up.

I ran the print_r and what I got was this, it doesnt say me so much but maybe you can outread something from it:P

stdClass Object ( 
[term_id] => 18 
[name] => Kategori1 
[slug] => kategori1 
[term_group] => 0 
[term_taxonomy_id] => 18 
[taxonomy] => faq-category 
[description] => 
[parent] => 0 
[count] => 3 
[cat_ID] => 18 
[category_count] => 3 
[category_description] => 
[cat_name] => Kategori1 
[category_nicename] => kategori1 
[category_parent] => 0 ) 

stdClass Object ( 
[term_id] => 19 
[name] => Kategori2 
[slug] => kategori2 
[term_group] => 0 
[term_taxonomy_id] => 19 
[taxonomy] => faq-category 
[description] => 
[parent] => 0 
[count] => 3 
[cat_ID] => 19 
[category_count] => 3 
[category_description] => 
[cat_name] => Kategori2 
[category_nicename] => kategori2
[category_parent] => 0 )

and one more std object for the  category no 3 i guess=)

What that says is, for example, in the first run you got the category whose id is 18, and in the second run you got the category whose id is 19. Just to show you how you can get information about a category from the object $category.

There might be a syntax error somewhere in the $args. I will test it out when I get a chance later today. I just have to get some work done first.

1 Like

Okey cool, appreciate it=)

It turns out I couldn’t use WP_query with custom post types and custom taxonomies together, I had to use query_posts(), but I got something to work.

$categories = get_categories( array('taxonomy' => 'faq_category') ); 

                foreach ( $categories as $category ) : 
                                        
                    $cat_name = $category->name;
                    
                    echo '<h2>' . $cat_name . '</h2>';
                    
                    query_posts( array( 'post_type' => 'questions', 'faq_category' => $cat_name ) );    
                    
                    if (have_posts()) : while (have_posts()) : the_post();
                            the_title(); 
                            the_content();
                        endwhile;
                    endif;    
                 wp_reset_query();
                 
                endforeach;     

Try this, replacing the_title(); and the_content(); with what you want to display, and see if it works for you.

1 Like

EDIT: spelling mistake

Your code works perfect, thanks A LOT!=)

You’re welcome. Glad it worked. :smiley:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.