• Hi all,

    We use a custom post type called “clients” to display our portfolio, which works perfect. This post type has a (category-like) taxonomy “services” to divide these client posts in two categories. The “query_posts” is set to 50 posts per page so only the 50 latest projects are shown. You can see this page here.

    We provide a filter at the top of the page so visitors can choose between the two “services” and show only those posts. The problem is, the template for “taxonomy-services.php” does not work with query_posts, so we have no way to determine how many posts should be shown. By default, WP shows only the 10 latest posts for the selected taxonomy/category.

    This is the code we use:

    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>
    
    <!-- projects -->
    <div class="projects">
    <ul class="hover_block">
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    	<li><a href="<?php echo get_permalink(); ?>"><?php the_post_thumbnail( array(200,140) ); ?><h5><?php the_title(); ?></h5><?php echo cc_get_the_term_list($post->ID, 'services', ' ', ', ', '', 0); ?></a></li>
    <?php endwhile; wp_reset_query(); ?>
    </ul>
    </div>

    As said, this works fine, but we would like to display more than the 10 latest posts (in fact, all posts if possible). We need some sort of overwrite for the WP defaults set in wp-admin. Any advice on how to accomplish this for custom taxonomies?

    Thanks in advance,
    Stef

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this in your theme’s functions.php:

    function my_post_queries( $query ) {
      // not an admin page and it is the main query
      if (!is_admin() && $query->is_main_query()){
    
        if(is_tax()){
          // show 50 posts on custom taxonomy pages
          $query->set('posts_per_page', 50);
        }
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    This only works if you don’t have a query_posts on the loop.

    Thread Starter Stef Verbeeck

    (@twizted)

    Thanks keesiemeijer for your input.

    Unfortunately, when I insert this in my functions.php; reloading the page displays following error:

    Fatal error: Call to undefined method WP_Query::is_main_query() in /var/www/html/wp-content/themes/v21/functions.php on line 80

    🙁

    Thread Starter Stef Verbeeck

    (@twizted)

    Quick update: removing the first part seems to fix the fatal error:

    function my_post_queries( $query ) {
    
    	if(is_tax()){
          // show 50 posts on custom taxonomy pages
          $query->set('posts_per_page', 50);
        }
      }
    add_action( 'pre_get_posts', 'my_post_queries' );

    Thanks keesiemeijer & Stef, this updated code works for me.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Set number of posts for custom taxonomy’ is closed to new replies.