On my website I have a custom post type called 'theme'. I wanted the homepage to display an archive of themes. For this the only approach that I could think of was to create a page template with a custom wp_query and loop in it. Here's the code:

<?php global $wp_query; $wp_query = new WP_Query( array('post_type' => 'theme','post_status' => 'publish', 'paged' => get_query_var('page') ) ); ?>
        <?php if ( $wp_query->have_posts() ) : ?>
            <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>

                    <?php get_template_part( 'content', 'theme-widget' ); ?>
            <?php endwhile; ?>
            <?php wptrc_paging_nav(); ?>
        <?php else : ?>
            <?php get_template_part( 'no-results', 'index' ); ?>
        <?php endif; ?>

I have a page created with this template as my homepage.

I also wanted my users to be able to change the order of items with query variables so I used this code:

function wptrc_filter_query( $query ) {
    if( is_main_query() && (get_query_var('post_type') == 'theme' || is_tax('theme_cat')) && !is_admin() ) {
        $order = get_query_var('theme_order');
        $query->set('order', $order);
add_action('pre_get_posts', 'wptrc_filter_query', 9999);

function wptrc_query_vars( $query_vars ) {
    $query_vars[] = 'theme_order';
    $query_vars[] = 'theme_orderby';
    return $query_vars;
add_filter( 'query_vars', 'wptrc_query_vars' );

The problem: Whenever I try passing the query variables to my homepage like this: http://mydomain.com/?theme_order=ASC my blog page shows up instead of the theme archive.

For some reason when I use the query variables WordPress loads up the index.php file instead of the page template I created. This is not limited to custom query variables. order and orderby also give the same result.

Any help would be appreciated. Thanks in advance.

  • 1
    I don't have a solution but I believe it's because adding ?var= to the root url is reserved for the search results, and the search always redirects to the index page. – gdaniel Jul 11 '14 at 15:18
  • Maybe that's the problem. I'll try adding a new rewrite rule instead. Thanks – Hasan Akhtar Jul 11 '14 at 15:23

Your Answer

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

Browse other questions tagged or ask your own question.