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.

Isn't there a permalink structure that will essentially list out all categories of a certain post type?

function create_faqs_post_type() {
    register_post_type( 'faqs',
        array(
            'labels' => array(
                'name' => __( 'FAQs' ),
                'singular_name' => __( 'FAQ' )
            ),
        'public' => true,
        'menu_position' => 5,
        'rewrite' => array('slug' => 'the-faqs')
        )
    );
}
add_action( 'init', 'create_faqs_post_type' );

function create_faq_taxonomy() {
    register_taxonomy(
        'faqs_categories',
        'faqs',
        array(
            'hierarchical' => true,
            'label' => 'FAQs Categories',
            'query_var' => true
        )
    );
}
add_action( 'init', 'create_faq_taxonomy' );

This is the code I've using to register the custom post type faqs and then register a taxonomy for it.

Isn't there a permalink structure that will essentially automatically list out all faqs of a certain taxonomy? Or do I need to create a custom template and query it specifically?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

@dcolumbus

You can do a permalink rewrite when you register the taxonomy using the following:

'rewrite' => array( 'slug' => 'faqcategories', 'with_front' => false ),

Then site.com/faqcategories should pull them and site.com/faqcategories/easy should get them for you for the 'easy' term.

If I'm understanding you correctly.

share|improve this answer
    
So let's say I have a CPT called Books ... then I have a taxonomy called Genre and a genre of Horror ... What I'm essentially trying to do is find out if I can list all "horror" books on a single page without having to create a custom Template for that query. mysite.com/books/horror <- shows all books associated with the Horror taxonomy. –  dcolumbus Mar 26 '12 at 3:32
    
Christopher? Are you still around? –  dcolumbus Apr 3 '12 at 2:26
    
Sorry, been hiking. Jonathan Christopher did a good write up on this a while ago. You can find it here: mondaybynoon.com/20110520/…. Note that the taxonomy should be registered before the post type (that screwed me up in the past). –  Christopher Apr 7 '12 at 16:59
    
What on earth? I've ALWAYS registered the CPT before registering the associated Taxonomy. What does that cause? –  dcolumbus Apr 17 '12 at 17:25
add comment

You can use something like this:

<?php
$catArgs = array(
        'taxonomy'=>'faqs_categories'
        // post_type isn't a valid argument, no matter how you use it.
        );
$categories = get_categories('taxonomy=faqs_categories&post_type=faqs'); ?>
<?php foreach ($categories as $category) : ?>
  <div class="faqs-cat"><?php echo $category->name; ?></div>
    <?php
    $postArgs = array(
        'orderby' => 'title',
        'order' => 'ASC',
        'post_type'=>'faqs',
        'cat'=>$category->cat_ID,
        'tax_query' => array(
                array(
                    'taxonomy' => 'faqs_categories'
                )
            )
        );
     query_posts($postArgs) ?>
    <ul>
         <?php while(have_posts()): the_post(); ?>
        <li><a><?php the_title() ?></a></li>
         <?php endwhile; ?>
    </ul>
<?php endforeach; ?>
<?php wp_reset_query();

Edited but original source here. Good luck, I hope this helps.

share|improve this answer
    
Okay, yeah... so there's no permalink structure that will give me the result I'm looking for. It's going to have to be a custom template? –  dcolumbus Mar 26 '12 at 3:17
    
You could possibly implement this as a filter, but I don't know of any other ways, however I'm not saying that there isn't an alternative. I've had to use this implementation a handful of times, as I haven't found a quicker alternative. Cheers –  David Mar 26 '12 at 3:21
add comment

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.