0

I have a Custom Post Type called "book" and a custom taxonomy for this CPT called "books". One term's slug of this taxonomy is "accepting-yourself". Here's the code of the "accepting-yourself" archive page (URL: my_site/books/accepting-yourself/):

if ( have_posts() ) : 
    while ( have_posts() ) : the_post(); 
    echo get_the_title();
    endwhile;
endif;

This should return all the "book" posts in custom taxonomy "books", that's 8 posts. Instead it returns only one.

Just in case here's the code defining my custom post type and custon taxonomy:

/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 
CUSTOM POST TYPE : BOOK
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

function my_custom_post_book() {
    $labels = array(
        'name'               => _x( 'Books', 'post type general name' ),
        'singular_name'      => _x( 'Book', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'book' ),
        'add_new_item'       => __( 'Add New Book' ),
        'edit_item'          => __( 'Edit Book' ),
        'new_item'           => __( 'New Book' ),
        'all_items'          => __( 'All Book' ),
        'view_item'          => __( 'View Book' ),
        'search_items'       => __( 'Search Book' ),
        'not_found'          => __( 'No books found' ),
        'not_found_in_trash' => __( 'No books found in the Trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Books'
    );
    $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our books specific data',
        'public'        => true,
        'menu_position' => 4,
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields'),
        'has_archive'   => true,
    );
    register_post_type( 'book', $args );    
}
add_action( 'init', 'my_custom_post_book' );

function my_updated_messages_book( $messages ) {
    global $post, $post_ID;
    $messages['book'] = array(
        0 => '', 
        1 => sprintf( __('Book updated. <a href="%s">View book</a>'), esc_url( get_permalink($post_ID) ) ),
        2 => __('Custom field updated.'),
        3 => __('Custom field deleted.'),
        4 => __('Book updated.'),
        5 => isset($_GET['revision']) ? sprintf( __('Book restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
        6 => sprintf( __('Book published. <a href="%s">View book</a>'), esc_url( get_permalink($post_ID) ) ),
        7 => __('Book saved.'),
        8 => sprintf( __('Book submitted. <a target="_blank" href="%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
        9 => sprintf( __('Book scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview book</a>'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
        10 => sprintf( __('Book draft updated. <a target="_blank" href="%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    );
    return $messages;
}
add_filter( 'post_updated_messages', 'my_updated_messages_book' );


/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 
CUSTOM BOOK TAXONOMY
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

$labels = array(
    'name'                          => 'Books',
    'singular_name'                 => 'Book',
    'search_items'                  => 'Search Books',
    'popular_items'                 => 'Popular Books',
    'all_items'                     => 'All Books',
    'parent_item'                   => 'Parent Book',
    'edit_item'                     => 'Edit Book',
    'update_item'                   => 'Update Book',
    'add_new_item'                  => 'Add New Book',
    'new_item_name'                 => 'New Book',
    'separate_items_with_commas'    => 'Separate Books with commas',
    'add_or_remove_items'           => 'Add or remove Books',
    'choose_from_most_used'         => 'Choose from most used Books'
    );

$args = array(
    'label'                         => 'Books',
    'labels'                        => $labels,
    'public'                        => true,
    'hierarchical'                  => true,
    'show_ui'                       => true,
    'show_in_nav_menus'             => true,
    'args'                          => array( 'orderby' => 'term_order' ),
    'rewrite'                       => array( 'slug' => 'books', 'with_front' => false ),
    'query_var'                     => true
);

register_taxonomy( 'books', 'book', $args );
1
  • What is the template file being used to display the query? What is the full code in that template file? What have you done to debug? Commented Jan 10, 2014 at 15:08

2 Answers 2

0

Your code should hook the CPT registration to init, though I don't know if that is part of the issue.

Your code is relatively simple, and it works just fine for me when I test it.

Unless you've altered the main query, on the /books/accepting-yourself archive page the Loop you've posted should return:

  1. The published posts
  2. In your "book" CPT
  3. In your "accepting-yourself" term
  4. Limited to the number you've selected in "Settings -> Reading" under "Blog pages show at most"

It really sounds like item #4 is the problem. If you can verify that that is not it, then my next guess would be that something is already altering the main query and setting the posts_per_page argument to 1-- probably a poorly written callback for the pre_get_posts hook.

As mentioned, I tested your code and I do not get any unexpected behavior. Without more information, that is all I've got. If you can edit more detail into the question I'll see what else I can do.

1
  • Oh dear, the issue was in #4: "Blog pages show at most" was set to 1. I suppose that's the moment when I go hide myself in the forest.
    – drake035
    Commented Jan 10, 2014 at 15:48
1

The problem using the native Reading Settings is that it effects the main loop.

What you can do is use pre_get_posts with a conditional tag to control how many posts in a CPT or Taxonomy archive are displayed:

You can modify this code with the conditional for your CPT or taxonomy archive page:

add_action( 'pre_get_posts', 'wpsites_taxonomy_archive_limit' );
function wpsites_taxonomy_archive_limit( $query ) {

if( $query->is_main_query() && !is_admin() && is_tax( 'books' ) ) {
    $query->set( 'posts_per_page', '8' );
}

}

The above code displays 8 posts in the custom taxonomy type archive, book.

For a custom post type archive, use something like this:

add_action( 'pre_get_posts', 'wpsites_cpt_archive_limit' );
function wpsites_cpt_archive_limit( $query ) {

if( $query->is_main_query() && !is_admin() && is_post_type_archive( 'book' ) ) {
    $query->set( 'posts_per_page', '8' );
}

}

Source http://codex.wordpress.org/Conditional_Tags#A_Taxonomy_Page

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.