Pre_get_posts Gives 404 on Custom Post Type

I’m trying to filter the query on a sub page called “Ask Question” to list all of the recent questions submitted by users, but I get a 404 when going to the page. Flushing the permalink structure did not fix the issue. It is currently set to Month and Name.

Creating a new question works properly and I’m able to access the new posts. The custom taxonomies work correctly as well, however the archive pages do not.

Read More

This is the code that I’m using.

Post type

function question_post_type() {
    $labels = array(
        'name'                => _x( 'Questions', 'Post Type General Name', 'text_domain' ),
        'singular_name'       => _x( 'Question', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'           => __( 'Questions', 'text_domain' ),
        'parent_item_colon'   => __( 'Parent Question:', 'text_domain' ),
        'all_items'           => __( 'All Questions', 'text_domain' ),
        'view_item'           => __( 'View Question', 'text_domain' ),
        'add_new_item'        => __( 'Add New Question', 'text_domain' ),
        'add_new'             => __( 'New Question', 'text_domain' ),
        'edit_item'           => __( 'Edit Question', 'text_domain' ),
        'update_item'         => __( 'Update Question', 'text_domain' ),
        'search_items'        => __( 'Search questions', 'text_domain' ),
        'not_found'           => __( 'No questions found', 'text_domain' ),
        'not_found_in_trash'  => __( 'No questions found in Trash', 'text_domain' ),
    );

    $rewrite = array(
        'slug'                => '/ask/question',
        'with_front'          => true,
        'pages'               => true,
        'feeds'               => true,
    );

    $args = array(
        'label'               => __( 'question', 'text_domain' ),
        'description'         => __( 'Question', 'text_domain' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'author', 'comments', 'revisions', 'custom-fields', 'page-attributes', ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 20,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'rewrite'             => $rewrite,
        'capability_type'     => 'post',
    );

    register_post_type( 'question', $args );
}

add_action( 'init', 'question_post_type', 0 );

Taxonomy

if ( ! function_exists('custom_question_taxonomy') ) {

    // Register Custom Taxonomy
    function custom_question_taxonomy()  {
        $labels = array(
            'name'       

            => _x( 'Question Types', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'              => _x( 'Question Type', 'Taxonomy Singular Name', 'text_domain' ),
        'menu_name'                  => __( 'Question Type', 'text_domain' ),
        'all_items'                  => __( 'All Question Types', 'text_domain' ),
        'parent_item'                => __( 'Parent Question Type', 'text_domain' ),
        'parent_item_colon'          => __( 'Parent Question Type:', 'text_domain' ),
        'new_item_name'              => __( 'New Question Type', 'text_domain' ),
        'add_new_item'               => __( 'Add Question Type', 'text_domain' ),
        'edit_item'                  => __( 'Edit Question Type', 'text_domain' ),
        'update_item'                => __( 'Update Question Type', 'text_domain' ),
        'separate_items_with_commas' => __( 'Separate Question Types with commas', 'text_domain' ),
        'search_items'               => __( 'Search Question Type', 'text_domain' ),
        'add_or_remove_items'        => __( 'Add or remove Question Types', 'text_domain' ),
        'choose_from_most_used'      => __( 'Choose from the most used Question Types', 'text_domain' ),
    );

    $rewrite = array(
        'slug'                       => 'question-type',
        'with_front'                 => false,
        'hierarchical'               => true,
    );

    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'rewrite'                    => $rewrite,
    );

    register_taxonomy( 'question-type', 'question', $args );
}


    add_action( 'init', 'custom_question_taxonomy', 0 );

}

Query

function question_posts_query( $query ) {

    if ( $query->is_post_type_archive( 'question' ) || $query->is_page(3001) && $query->is_main_query() ) {
        $query->set( 'post_type', 'question' );
    }
    return $query;
}
add_filter( 'pre_get_posts', 'question_posts_query' );

My page loop

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

    <div class="entry">
        <div class="post-content">

          <?php the_content(__('Read More &raquo;', 'gp_lang')); ?>

          <?php the_terms( $post->ID, 'question-type', '<div class="category"><span>Category: </span>', '', '</div>' ); ?>
        </div>
    </div>

    <?php endwhile; ?>
<?php endif; ?>

Related posts

Leave a Reply

1 comment

  1. Changing a query for the page post type via pre_get_posts is a bit tricky, and maybe not worth the potential issues involved. If you inspect the generated SQL, it’s looking for your requested pagename in the question post type, which is why you get the 404.

    The solution is to delete the page, so WordPress will load a post type archive by default, or keep the page and use WP_Query within the template to load the custom posts.