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.

I am working with custom post types in the bones theme from themble. There, I want to create a link list and therefor i've created a custom post type, called links.

This custom post type has a custom taxonomy, called links_categories. Now I would like to display all links on one page, like this:

Link Category 1

  • Post 1
  • Post 2
  • ...

Link Category 2

  • Post 1
  • Post 2
  • ...

While researching on how to achieve this dynamically, i tried this two solutions:

Loop through custom taxonomies and display posts

Custom post type multiple loop by taxonomy term

Strangely enough I had the same issue with both versions. It seems like WP_Query is not getting any posts - I just get the slug of the custom category term.

Here is what I have for now:

<?php get_header(); ?>

        <div id="content">

            <div id="inner-content" class="wrap clearfix">

                <div id="main" class="eightcol first clearfix" role="main">

                    <h1 class="archive-title h2"><?php post_type_archive_title(); ?></h1>

                    <?php
                        $post_type = 'links';

                        // Get all the taxonomies for this post type
                        $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );

                        foreach( $taxonomies as $taxonomy ) : 

                            // Gets every "category" (term) in this taxonomy to get the respective posts
                            $terms = get_terms( $taxonomy );

                            foreach( $terms as $term ) : 

                                $wp_query = new WP_Query( array(
                                    'taxonomy' => $taxonomy,
                                    'term' => $term->slug,
                                    'posts_per_page' => '-1'
                                    )
                                );

                                if( $wp_query->have_posts() ) :

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

                                        <article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">

                                            <header class="article-header">


                                            </header> <!-- end article header -->

                                            <section class="entry-content clearfix">

                                                <div class="threecol first">
                                                    <?php // show the post thumbnail
                                                    if ( has_post_thumbnail() ) { ?>

                                                        <a class="fadeit" href="<?php the_permalink(); ?>">
                                                            <?php the_post_thumbnail(); ?>
                                                        </a>

                                                    <?php } ?>
                                                </div>

                                                <div class="ninecol">
                                                    <strong><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></strong>
                                                    <?php the_content(); ?>
                                                </div>

                                            </section> <!-- end article section -->

                                            <footer class="article-footer">

                                            </footer> <!-- end article footer -->

                                        </article> <!-- end article --> <?php

                                    endwhile;

                                    else : ?>

                                        <article id="post-not-found" class="hentry clearfix">
                                            <header class="article-header">
                                                <h1><?php _e("Oops, Post Not Found!", "bonestheme"); ?></h1>
                                            </header>
                                            <section class="entry-content">
                                                <p><?php _e("Uh Oh. Something is missing. Try double checking things.", "bonestheme"); ?></p>
                                            </section>
                                            <footer class="article-footer">
                                                <p><?php _e("This is the error message in the custom posty type archive template.", "bonestheme"); ?></p>
                                            </footer>
                                        </article> <?php

                                endif;

                            endforeach;

                        endforeach;
                    ?>

                </div> <!-- end #main -->

                <?php get_sidebar(); ?>

            </div> <!-- end #inner-content -->

        </div> <!-- end #content -->
share|improve this question
1  
You mean that if you add var-dump($wp_query); right before your if( $wp_query->have_posts() ) : you get an object with nothing in it but the slug term? Also, WordPress uses $wp-query. You are clobbering that object, so choose another name for your new object. –  s_ha_dum Feb 10 '13 at 19:55
    
@s_ha_dum - I already tried to use another name instead of $wp_query, but this doesn't change anything. Here is my var_dump: [link]pastebin.com/eq0wgVQD –  Nubster Feb 11 '13 at 0:42
    
I didn't expect using a different variable name to fix the problem. It is just good practice not to clobber core created variables. I see the problem now. –  s_ha_dum Feb 11 '13 at 1:03
add comment

1 Answer

up vote 0 down vote accepted

If you look at your var_dump, near the end, you will see the actual query being used.

SELECT wp_posts.* 
FROM wp_posts 
INNER JOIN wp_term_relationships 
ON (wp_posts.ID = wp_term_relationships.object_id) 
WHERE 1=1 
AND ( wp_term_relationships.term_taxonomy_id IN (93) ) 
AND wp_posts.post_type IN ('post', 'page', 'attachment') 
AND (wp_posts.post_status = 'publish' 
    OR wp_posts.post_author = 1 
    AND wp_posts.post_status = 'private') 
GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC

You haven't told the query to look in your custom post type. Notice the wp_posts.post_type IN ('post', 'page', 'attachment')? This is obvious in your code as well, but I nonetheless didn't notice until I saw the var_dump :(

You need to alter your query so that you are searching your custom type, and your 'taxonomy' query is off.

$wp_query = new WP_Query( 
    array(
       'post_type' => $post_type, // should be 'links', correct?
       $taxonomy => $term->slug,
       'posts_per_page' => '-1'
    )
);

Or...

$wp_query = new WP_Query( 
    array(
       'post_type' => $post_type, // should be 'links', correct?
       'tax_query' => array(
          array (
            'taxonomy' => $taxonomy,
            'field' => 'slug',
            'term' => $term->slug,
          )
       ),
       'posts_per_page' => '-1'
    )
);

I am sticking my neck out some but I think that will do it. I'd have to set up a lot of stuff in my database to actually test that-- post type, terms, add some data, etc-- but that looks right. If it is off, hopefully it isn't off too badly.

You do realize this isn't going to scale very well? With the nested loops and the multiple queries this is not very efficient, and could grind to a crawl if your taxonomy gets very big.

Reference: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

share|improve this answer
    
I was wrapping my head around the fact that I didn't tell the query from which taxonomy exactly it should get the data. First, I tried your second solution, but this did not work either - then i gave the first one a try and it definitely made my day. Thank you very much for helping me out of this! –  Nubster Feb 11 '13 at 8:57
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.