WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Ok so I have multiple different page templates with a main query that I want to filter the loop for. In my example I try to use a function to filter the main loop of my site and also the main loop for the page 'new'. Now this works fine for the main page but makes the 'new' page return a 'page not found' not taking the custom template that's assigned to it. What's the right way to do this?

function fod_filter_main_query( $query ) {

    if( is_front_page() && is_main_query() ) {

        $query->set('meta_key', 'hotness');
        $query->set('meta_value', 0 );
        $query->set('meta_compare', '>');
        $query->set('orderby', 'meta_value_num' );
        $query->set('order', 'desc');
        $query->set('cat', -1 );
        $query->set('posts_per_page', 16 );
        $query->set('post_type', array( 'post','fod_videos','fod_music','fod_articles', 'fod_albums'  ));

    } elseif  ( is_page('new') && is_main_query() ) {

        $query->set('cat', -1 );
        $query->set('order', 'asc');
        $query->set('posts_per_page', 16 );
        $query->set('post_type', array( 'post','fod_videos','fod_music','fod_articles', 'fod_albums'  ));


add_action('pre_get_posts', 'fod_filter_main_query', 9999);
share|improve this question

You can't alter a page query via pre_get_posts (in any fashion I've tried) to something other than a page query. If you look at the generated SQL, it's trying to query all your post types for a pagename of new. If you set pagename to null, it'll return posts, but it will disregard any taxonomy parameters you try to set.

Even if you could successfully alter the query, it would no longer be a page and wouldn't load a page template.

I think the simplest way to handle this is to just create a new query in your template via WP_Query.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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