0

Many answers on this particular question, even on this site, but none of them fit me. Yes, I did all the things I know, and I got from previous answers, but my custom post type archive is not working:

<?php
function project_register_cpt_books() {

    $labels = array(
        'name'                  => __( 'Books', 'textdomain' ),
        'singular_name'         => __( 'Book', 'textdomain' ),
        'name_admin_bar'        => __( 'Books', 'textdomain' ),
        'add_new'               => __( 'Add New', 'textdomain' ),
        'add_new_item'          => __( 'Add New Book', 'textdomain' ),
        'edit_item'             => __( 'Edit Book', 'textdomain' ),
        'new_item'              => __( 'New Book', 'textdomain' ),
        'view_item'             => __( 'View Book', 'textdomain' ),
        'search_items'          => __( 'Search Books', 'textdomain' ),
        'not_found'             => __( 'No Book found', 'textdomain' ),
        'not_found_in_trash'    => __( 'No Book found in Trash', 'textdomain' ),
        'parent_item_colon'     => __( 'Parent Book:', 'textdomain' ),
        'menu_name'             => __( 'Books', 'textdomain' ),
    );

    $args = array(
        'labels'                => $labels,
        'hierarchical'          => false,
        'description'           => __( 'To get books information', 'textdomain' ),
        'supports'              => array( 'title', 'editor' ),
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'show_in_nav_menus'     => true,
        'publicly_queryable'    => true,
        'exclude_from_search'   => false,
        'has_archive'           => true,
        'query_var'             => true,
        'can_export'            => true,
        'rewrite'               => array( 'slug' => 'books' ),
        'capability_type'       => 'post'
    );
    register_post_type( 'books', $args );

    /**
     * To Activate Custom Post Type Single page.
     * @author  Bainternet
     * @link http://en.bainternet.info/2011/custom-post-type-getting-404-on-permalinks
     * -----------
     */
    $set = get_option('post_type_rules_flased_books');
    if ($set !== true){
        flush_rewrite_rules(false);
        update_option('post_type_rules_flased_books',true);
    }

}
add_action( 'init', 'project_register_cpt_books' );

Activation/Deactivation of theme, flashing permalink, checking both Default and PostName permalink, changing slug, changing/commenting out rewrite parameter... all failed. There's no way my archive-books.php is working. It's falling back to archive.php by all the means.

How can I fix this?

0

I found my problem, because I's causing the issue by myself unknowingly. I'm posting the answer for you to debug if something like this can cause the issue:

I's using the following code to include all the CPT entries to the default loop. And that's causing the issue. It's directing the posttype archive to default post archive (archive.php).

<?php
/**
 * Adding the Book entries to the loop.
 * @param object $query WordPress' default query object.
 * ------------------------------------------------------------------------------
 */
function project_add_book_entries_to_query( $query ) {
    if ( $query->is_main_query() && !is_admin() ) {
        $query->set( 'post_type', array('post', 'books') );
    }
}
add_action( 'pre_get_posts', 'project_add_book_entries_to_query' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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