3

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.

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; ?>
2
  • Do the pages resolve without the filter being applied? Do you get a 404 on both the custom post type's archive index page and static page ID 3001? (Note: watch your Boolean conditions. You've got an AND in series with an OR. I would suggesting putting the OR conditionals in parentheses. Feb 18, 2013 at 20:18
  • @ChipBennett Yes, the page resolves without the filter. I do get a 404 on the post type archive page.
    – Matt Av
    Feb 18, 2013 at 21:06

1 Answer 1

2

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.

5
  • Using wp_query on the page does work, though its not the best solution in the long run for dynamically displaying content with flexibility. The archive not working is still a problem.
    – Matt Av
    Feb 19, 2013 at 15:56
  • why do you have a page where you want a post type archive? maybe I misunderstand what you're trying to do, the path of your page and how it relates to your post type archive isn't clear to me.
    – Milo
    Feb 19, 2013 at 16:02
  • The Ask page displays a list of questions submitted by the user along with a submission form. Its designed as a Q/A. I want to use the default archive for the question post type to give users access to the older questions (answered or not) listed by the month and year.
    – Matt Av
    Feb 19, 2013 at 17:37
  • 1
    for the default post type archive, change your slug to 'ask/question' without the leading slash. as I mention in my answer, the pre_get_posts filter to load posts on the page post type will never work quite right, though I don't understand why WP_Query wouldn't work for you in this case, perhaps you could elaborate on that.
    – Milo
    Feb 19, 2013 at 19:10
  • How would you remove the current page ID from this main query? That's why I get 404 ... this part in SQL query: AND (wp_posts.ID = '2') which is current page ID ...
    – trainoasis
    Jul 7, 2017 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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