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'm just starting to wrap my head around custom post types and for some reason can't get any of the custom post types to display in the loop. I've found some queries online but none of them are in closed php statements and I'm not sure how to integrate them. Here's what I have:

// add custom post type
add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'band_artists',
        array(
            'labels' => array(
                'name' => __( 'Artists' ),
                'singular_name' => __( 'Artist' )
            ),
        'public' => true,
        'has_archive' => true,
        'supports' => array( 'title', 'editor', 'comments', 'excerpt', 'custom-fields', 'thumbnail' ),
        'rewrite' => array('slug' => 'artists'),
        'taxonomies' => array('category', 'post_tag')
        )
    );
}

and query with a randomizer... This was working when these were just plain ol' posts (minus the 'post_type' obviously).

<?php query_posts(array(
    'post_type' => 'band_artists',
    'showposts' => 1,
    'orderby' => 'rand',
    'category' => '5'
    ));
    if ( have_posts() ) : while ( have_posts() ) : the_post(); 
?>

<div id="post-<?php the_ID(); ?>" class="artistSingle">

    <?php if (has_post_thumbnail( $post->ID ) ): ?>
    <div id="artistImgSingle"><?php the_post_thumbnail('medium'); ?></div>
    <?php endif; ?>

    <p class="artistName"><?php the_title(); ?></p>
    <p>
        <?php if(get_post_meta($post->ID, 'band', true)): ?>
            <strong>Band:</strong> <?php echo get_post_meta($post->ID, 'band', true); ?><br />  
        <?php endif; ?>
        <?php if(get_post_meta($post->ID, 'gear', true)): ?>
            <strong>Gear:</strong> <?php echo get_post_meta($post->ID, 'gear', true); ?><br>
        <?php endif; ?> 
        <?php if(get_post_meta($post->ID, 'website', true)): ?>
            <strong>Website:</strong> <a href="http://<?php echo get_post_meta($post->ID, 'website', true); ?>" target="_blank"><?php echo get_post_meta($post->ID, 'website', true); ?></a>
        <?php endif; ?>
    </p>
    <?php the_content(); ?>

    <div>
        <?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', '2012' ) . '</span>', 'after' => '</div>' ) ); ?>
    </div>

</div>

<?php endwhile; // end of featured large loop ?>
<?php else : ?>
<?php endif; ?>
share|improve this question
1  
So what happens? You get a parse error? Or everything runs fine but no posts are displayed? And category isn't a valid query arg, so it won't do anything! –  TheDeadMedic Nov 8 '12 at 10:04
    
No error, it just doesn't display anything but the structure of the page. No database data. category was working just fine when it was pulling standard post types. –  Kristina Childs Nov 8 '12 at 17:17
    
I was wrong - category maps to its taxonomy query var - are there definitely band_artists in the category with ID 5? –  TheDeadMedic Nov 8 '12 at 17:24
    
Band_artist is the custom post type key. And yes, I triple checked to make sure the cat ID is correct. I take it by your response that this query should in fact work? The pages I am using to display them are called archive-band_artists.php and single-band_artists.php. Neither work, but when I convert these posts back to a standard post type they show up. –  Kristina Childs Nov 8 '12 at 18:11
    
Even when I remove the category arg it doesn't work, so I tested to see what file this is using to pull up. Turns out it wasn't using the archive-band_artists.php file at all, it was using the old category-artists.php used to display the query for standard post types. Isn't wordpress supposed to pull the key for custom post types? Or is it the slug? It's working now that I've edited category-artists.php... sort of. It's not pulling the header at all, so there are no styles, navbar, footer, etc. –  Kristina Childs Nov 8 '12 at 18:19

2 Answers 2

http://codex.wordpress.org/images/1/18/Template_Hierarchy.png nice visual breakdown of template hierarchy

share|improve this answer
    
I've seen that. However, in this page of the codex, they instruct you to use archive-{postype}.php. So that's what I did. codex.wordpress.org/Post_Types –  Kristina Childs Nov 9 '12 at 1:27
1  
Oh, I'm not disagreeing with you. But you may have a namespace conflict that is causing WP to deliver a category archive rather than your custom post_type archive. Is there a Category that has the same name as your custom post type? –  totels Nov 9 '12 at 12:15
    
The same slug, yes, but it is attached to that custom post type... carried over from the original setup. But it's supposed to look for a custom post type first and didn't want to make significant changes before I was sure this worked. sigh catch-22. Good point, though. I'll check it out. –  Kristina Childs Nov 9 '12 at 19:40
1  
Category path rewrites should be recalculated at the same time as all the other items. I will be the first to admit that there are many times when some setting in the DB related to permissions or rewrites gets easily messed up when working with custom post types. I usually end up setting up a sandbox WP install and testing the bare minimum of what I expect to work to see if what I think is broken is actually broken. –  totels Nov 11 '12 at 1:16
1  
I took your advice to solve a larger issue of the wordpress template hierarchy not working at all. It was using page.php template to display custom post type archive, but when I deleted all pages it would use nothing at all. I got it working in the sandbox so it's a conflict somewhere else. +1's to you for suggesting that... I may have banged my head a lot longer before doing that. –  Kristina Childs Nov 14 '12 at 19:31
up vote 1 down vote accepted

After much searching and edumication on custom post types I figured it out. I've also added things to make the specifications more granular.

// Add custom post type Artists 
function register_artists_post_type() {
    register_post_type('artists',array(
        'labels' => array(
                'name' => __( 'Artists' ),
                'singular_name' => __( 'Artist' ),
                'add_new' => __( 'Add Artist','Artist' ),
                'add_new_item' => __( 'Add New Artist' ),
                'edit_item' => __( 'Edit Artist' ),
                'new_item' => __( 'New Artist' ),
                'view_item' => __( 'View Artist' ),
                'search_items' => __( 'Search Artists' ),
                'not_found' => __( 'No Artists Found' ),
                'not_found_in_trash' => __( 'No Artists In Trash' ),
                'parent_item_colon' => ''
            ),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'has_archive' => true,
        'supports' => array( 'title','editor','excerpt','custom-fields','thumbnail' ),
        'rewrite' => array('slug' => 'artists'),
        'taxonomies' => array('large_feature','small_feature'),
        'capability_type' => 'post',
        'hierarchical' => false,
    ));
}
add_action('init','register_artists_post_type')

Register custom taxonomies (for newbies, that's a fancy way of saying categories... "Categories" within normal posts is just the default taxonomy)

// Add custom taxonomies for Artists post type
add_action('init','register_large_feature_taxonomy');
function register_large_feature_taxonomy() {
    register_taxonomy('large_feature',array('artists'),array(
        'hierarchical' => true, 
        'label' => 'Large Feature', 
        'public'=> true,
        'show_ui'=> true,
        'query_var'=> true,
        'rewrite' => false,
    ));
}   
add_action('init','register_small_feature_taxonomy');
function register_small_feature_taxonomy() {
    register_taxonomy('small_feature',array('artists'),array(
        'hierarchical' => true, 
        'label' => 'Small Feature', 
        'public'=> true,
        'show_ui'=> true,
        'query_var'=> true,
        'rewrite' => false,
    ));
}       

and finally, the query, as written in archive-artists.php

<?php query_posts(array(
    'large_feature' => 'artists_page_large', // this is the same as calling for a category within standard posts, where 'large_feature' is category and 'artists_page_large' is the ID/slug (either should work, I chose to use the slug to avoid going back to check ID numbers)
    'showposts' => 1,
    'orderby' => 'rand', // randomizes the posts each page load
    ));
    if ( have_posts() ) : while ( have_posts() ) : the_post(); 
?>

<div id="post-<?php the_ID(); ?>" class="artistSingle white">
    <!-- do stuff -->
</div>

<?php endwhile; endif; // end of featured large loop ?>



<div id="featArtistSm" class="white">

    <?php rewind_posts(); // start featured small loop ?> <!-- rewind allows for multiple loops on a single page -->
    <?php query_posts(array(
        'small_feature' => 'artists_page_small',
        'showposts' => 4,
        'orderby' => 'rand',
        ));
    ?>
    <?php if(have_posts()) : while (have_posts() ) : the_post(); ?>

    <div class="featArtistSmSng white">
        <-- do stuff -->
    </div>

    <?php endwhile; endif; // end of featured small loop ?>
</div>



<div id="featuredArtists">
  <div id="artistRoster" class="artistRoster">

    // query to pull all posts of 'artist' custom post type
    <?php query_posts('post_type=artists&orderby=title&order=asc&posts_per_page=-1'); ?>
        <?php if(have_posts()) : while (have_posts() ) : the_post(); ?>

        <p class="roster"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></p>

    <?php endwhile; endif; ?>

  </div>
</div>

Hopefully this helps someone else out! Since I originally had a category called Artists with the slug artists, I ended up deleting the categories altogether and using the slugs for the custom post type. It was much cleaner and eliminated the possibility of confusing the system. It turns out I also had a page called Artists with a template page I had deleted, but the page was still it the trash. Make sure to delete any conflicting items from the trash as well! Even if it's in trash, it's still in your database.

share|improve this answer

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.