Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

OK, so I asked this earlier and no one had any answer for me. I have a CPT for FAQs. FAQs are separated into 'categories.' I need the name of the 'category' then a every post within that 'category.'

Example:

Category 1

Title of Post 1 within Category 1

Content of Post 1 within Category 1

Title of Post 2 within Category 1

Content of Post 2 within Category 1

Category 2

Title of Post 1 within Category 2

Content of Post 1 within Category 2

Title of Post 2 within Category 2

Content of Post 2 within Category 2



The code that I now have is:

        <?// for a given post type, return all except 'uncategorized'
            $post_type = 'faq';
            $tax = 'category';
            $tax_terms = get_terms($tax, array('orderby' => 'id', 'order' => 'ASC', 'exclude' => '1'));
            if ($tax_terms) {
                foreach ($tax_terms as $tax_term) {
                    $args = array(
                        'post_type' => $post_type,
                        "$tax" => $tax_term->slug,
                        'post_status' => 'publish',
                        'posts_per_page' => - 1,
                        'orderby' => 'title',
                        'order' => 'ASC',
                        'caller_get_posts' => 1
                        ); // END $args
                    $my_query = null;
                    $my_query = new WP_Query($args);
                    if ($my_query->have_posts()) {
                        echo '<div class="aro"><div class="wrap"><h2>' . $tax_term->name . '</h2></div><div class="details"><div class="wrap">';

                        while ($my_query->have_posts()) : $my_query->the_post(); ?>

                                <div class="info">
                                    <h3><?php the_title(); ?></h3>
                                    <?php the_content(); ?>
                                </div>

        <?php
                        endwhile; } // END if have_posts loop
                        echo '</div></div></div>'; // Close 'details', 'wrap', & 'aro' DIVs
                wp_reset_query(); } // END foreach $tax_terms
            } // END if $tax_terms
        ?>



This code does list every 'category' individually, which I do want. However, every post within FAQs is listed under every 'category,' which is not what I want. Any ideas what I'm doing wrong?

share|improve this question
    
I'm not sure. I found this code while looking for my answer and tailored to my application. I did notice the "$tax" part and thought it didn't took right. Could that part of my problem? –  user3571316 Jul 11 at 21:10
    
It looks like one of the query arguments is not working, because the logic looks correct. Try removing the quotations from $tax and checking that all variables have the expected values. –  gdaniel Jul 11 at 21:19
    
That didn't change anything. Also, the 'categories' that I was were referring to, I believe they are categories and not taxonomies. If that is correct. Not sure about those two and how they are different. –  user3571316 Jul 11 at 21:23
    
Categories is a taxonomy -> codex.wordpress.org/Taxonomies Also, take a look at the codex for querying taxonomies with wp_query -> codex.wordpress.org/Class_Reference/… It's best if you understand how things work. –  gdaniel Jul 11 at 21:28
    
Thanks. And I agree with understanding. I now get that Categories is the taxonomy. And any category I select or create in the post is the taxonomy term. Obviously, $tax_term->name is the name of my term. I'm just having trouble understanding to only list the posts of the term, instead of all. –  user3571316 Jul 11 at 21:47

1 Answer 1

up vote 0 down vote accepted

You have double posted this question, and in your comment in the previous question you said that you don't know if you are using build-in categories or custom taxonomies.

Essentially, built-in categories and custom taxonomies are both Taxonomies, and the "categories" that you create in the back end are in actual fact just terms of the taxonomy 'category', so essentially, your query will be exactly the same for both build-in categories and custom taxonomies

If you need to know if you are dealing with categories or custom taxonomies within a post type, you can use get_object_taxonomies to retrieve these information. This function will also be used later to retrieve a list of categories or custom taxonomies to compile your custom query. Just change $post_type to the actual name of your post type, in your case faq

$taxonomy_names = get_object_taxonomies( '$post_type' );
   print_r( $taxonomy_names);

If you are using custom taxonomies, the following will be returned

Array ( [0] => TAXONOMY_NAME )

If you are using categories, the following will be returned

Array
(
    [0] => category
    [1] => post_tag
    [2] => post_format
)

Ok, done with the nitty gritty stuff. Lets get to the query. You are almost there. The correct way here is to use the taxonomy parameters within WP_Query to construct your query.

$args = array(
    'post_type' => 'POST TYPE OF WHICH POSTS YOU WANT TO RETRIEVE',
    'tax_query' => array(
        array(
            'taxonomy' => 'NAME OF YOUR TAXONOMY',
            'field' => 'WHICH FIELD TO USE TO RETRIEVE TERMS',
            'terms' => 'NAME OF YOUR TERM'
        )
    )
);
$query = new WP_Query( $args );

Another thing of note, caller_get_posts is longtime depreciated, and was replaced with ignore_sticky_posts

This is how your final query should look like. I just copied, corrected and tested your code as is, so if there are any other modifications that you need to make, feel free and adapt

<?php 
$post_type = 'faq';
$taxonomies = get_object_taxonomies( $post_type );

foreach ($taxonomies as $taxonomy){
    $terms = get_terms($taxonomy, array('orderby' => 'id', 'order' => 'ASC', 'exclude' => '1'));

    if ( !empty( $terms ) && !is_wp_error( $terms ) ){
        foreach ( $terms as $term ) {
            $args = array(
                'post_type'           => $post_type,
                'orderby'             => 'title',
                'order'               => 'ASC',
                'ignore_sticky_posts' => 1,
                'post_status'         => 'publish',
                'posts_per_page'      => - 1,
                'tax_query'           => array(
                    array(
                        'taxonomy'    => $taxonomy,
                        'field'       => 'slug',
                        'terms'       => $term->slug
                    )
                )
            );
            $my_query = null;
            $my_query = new WP_Query($args);
            if ($my_query->have_posts()) {
                echo '<div class="aro"><div class="wrap"><h2>' . $term->name . '</h2></div><div class="details"><div class="wrap">';

                    while ($my_query->have_posts()) : $my_query->the_post(); ?>

                        <div class="info">
                        <h3><?php the_title(); ?></h3>
                        <?php the_content(); ?>
                        </div>

                    <?php
                    endwhile; 
            } // END if have_posts loop
                echo '</div></div></div>'; // Close 'details', 'wrap', & 'aro' DIVs
            wp_reset_query(); 
        } // END foreach $terms
    }
}
?>
share|improve this answer
    
Thank you! Sorry about the double post. I deleted the other. I greatly appreciate your explanations and the updated code. –  user3571316 Jul 12 at 17:38
    
My pleasure, glad I could help. Enjoy :-) –  Pieter Goosen Jul 12 at 18:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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