• Hello all. I’m having some trouble displaying the posts in a custom post type on my portfolio site, and I’m wondering if someone can help.

    Here’s the deal. I have a custom post type called ‘work.’ Within that custom post type, I have a custom taxonomy called ‘project type’ where I have created several categories called photography, video, print design, etc. I set up a template called taxonomy.php which governs mydomain.com/work as well as mydomain.com/project-type/print-design/. The problem is that these pages only display the 5 most recent posts. I need them to display all the posts in the proper category (or in the case of mydomain.com/work, all of the posts in the custom post type regardless of category). How can I modify this code to display all of the posts?

    Here is the code I have used for taxonomy.php:

    <?php
    
    /**
     * Project Type Taxonomy Archive
     */
    
    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    ?>
    
    <h3 style="color:#EEEEEE"><?php echo apply_filters( 'the_title', $term->name ); ?> PROJECTS</h3>
    
    <ul class="thumbnails">
    	<div id="work">
    
    		<?php if ( !empty( $term->description ) ): ?>
    		<div class="archive-description">
    			<?php echo esc_html($term->description); ?>
    		</div>
    		<?php endif; ?>
    
    		<?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>
    
    		<div class="post">
    			<li class="span4">
    				<div class="thumbnail">
    				<a href="<?php the_permalink() ?>"><div class="tint"><?php the_post_thumbnail( 'featured-thumb' ); ?></div></a>
    				<br />
    				<div class="thumbcaption">
    					<a href="<?php the_permalink() ?>"><h3><?php the_title() ?></h3></a>
    					<p>	<?php the_excerpt(); ?></p>
    					<p><i><?php the_terms( $post->ID, 'work_project_type' ,  ' ' ); ?></i></p>
    				</div>
    				</div>
    			</li>
    		</div>
    
    		<?php endwhile; ?>
    
    		<div class="navigation clearfix">
    			<div class="alignleft"><?php next_posts_link('« Previous Entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Next Entries »') ?></div>
    		</div>
    
    		<?php else: ?>
    
    		<h2 class="post-title">No Projects in <?php echo apply_filters( 'the_title', $term->name ); ?></h2>
    		<div class="content clearfix">
    			<div class="entry">
    				<p>It seems there aren't any projects in <strong><?php echo apply_filters( 'the_title', $term->name ); ?></strong> right now. Check back later, I try to add new projects regularly.</p>
    			</div>
    		</div>
    
    		<?php endif; ?>
    	</div>
    	</div>
    </ul>

    Here’s the link to the work page and here’s a link to a category page. Thanks for your help.

Viewing 15 replies - 1 through 15 (of 17 total)
  • maybe if you put this in front of the query?

    <?php query_posts('posts_per_page=-0');?>

    Thread Starter mcography

    (@mcography)

    Thanks for your help, but unfortunately, that didn’t work. It just broke the thumbnail images…

    not sure if it’s related to pagination, but you might check this out
    http://wordpress.stackexchange.com/questions/1635/fixing-pagination-with-custom-taxonomy-archive

    Thread Starter mcography

    (@mcography)

    Unfortunately, that question was never resolved. I tried checking out some of the links that people suggested, but they did not work.

    Does anyone have any other ideas on how to modify my code so it displays all of the posts?

    What number is set under Settings > Reading > “Blog pages show at most” in your administrator section?

    Thread Starter mcography

    (@mcography)

    It’s set to 5. However, I do not want to increase it, because I want my main blog page to only display 5 posts.

    There’s got to be a way to force taxonomy.php to display all posts without messing with the main blog settings, I just can’t seem to figure it out…

    Thread Starter mcography

    (@mcography)

    Okay, I think I’m getting a little bit closer. I found this page about query_posts which helped me realize that I needed to add <?php query_posts( array ( 'post_type' => 'work', 'posts_per_page' => -1 ) ); ?>. So now the work page is loading correctly, but all of the category pages are loading all the work posts (because I’ve defined the post_type as work in that snippet I added).

    How can I generalize the code so that it makes the work page display all of the work posts and the category pages display only the relevant category posts?

    So, you need to set up a secondary query. If you see in your code, you’re using the main query:
    <?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>

    which is why I asked about the reading settings.

    At the top of your template file, instantiate a new instance of the WP_Query object and pass it the necessary parameters, your custom post type and the number of posts you want to display:

    $args = array('post_type' => 'work','post_count'=>whatever);
    $my_query = new WP_Query($args);

    You can further refine this query string (the $args array) if you want. Take a look at the documentation for WP_Query. Now, you can use $my_query as a substitute loop.

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

    So, when you call $my_query->the_post() in that while loop, you’re referencing the secondary loop you created, rather than the main loop. Understand?

    You might find this talk on the loops and queries useful too: http://wordpress.tv/2012/06/15/andrew-nacin-wp_query/

    are the work posts and other category posts using the same template?

    if so, maybe a simple conditional;

    <?php if(!is_category('your non-work-post-category')) { ?>
    
    <?php query_posts( array ( 'post_type' => 'work', 'posts_per_page' => -1 ) ); ?>.
    
    <?php } ?>

    not sure if this addresses the same issue as Andrew’s solution, I’m having a hard time understanding the secondary query but I intend to check it out, sounds like something I may need to do with something I’m working on..

    Deep, I don’t think query_posts() is appropriate what the OP is trying to do here as it modifies the main query.

    See this diagram from stack overflow: http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts

    I would also recommend watching the talk from Andrew Nacin that I posted the link to.

    thanks, then perhaps the same with get_posts?. the query I posted was from OP’s post. I was only trying to illustrate how he might call the custom post type in it’s own query, outside the loop (so without modifying it). Then let it use the same output as the other catgegories.

    however It could be I’m misunderstanding the goal..

    get_posts() is just a wrapper for WP_Query that returns an array.

    Thread Starter mcography

    (@mcography)

    Andrew, I tried to implement your suggestion, but that just made each page (mydomain.com/work and all the category pages mydomain.com/project-type/photography, etc.) load the first 5 posts of the work post type regardless of category. So I’m still confused. I really do appreciate you taking the time to help me, though. I watched Nacin’s video and looked at the WP_Query documentation, but I’m afraid it’s a bit advanced for me. I will look at it again to see if I can figure this thing out. In the meantime, if you have any other suggestions, I would welcome them gladly. Thanks again for your help.

    Deepbevel, yes the work posts and category posts are both using taxonomy.php. Thanks for your help, too.

    Thread Starter mcography

    (@mcography)

    Anyone have any other ideas?

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Template to Display All Posts in Custom Post Type and Custom Taxonomy’ is closed to new replies.