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>

                        $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(); ?>

                                                    <?php } ?>

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

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

                                            <footer class="article-footer">

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

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


                                    else : ?>

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




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

                <?php get_sidebar(); ?>

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

        </div> <!-- end #content -->
  • 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
    Commented Feb 10, 2013 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
    Commented Feb 11, 2013 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
    Commented Feb 11, 2013 at 1:03

1 Answer 1


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( 
       'post_type' => $post_type, // should be 'links', correct?
       $taxonomy => $term->slug,
       'posts_per_page' => '-1'


$wp_query = new WP_Query( 
       '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

  • 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
    Commented Feb 11, 2013 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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