title: WordPress - pre_get_posts in place of query_posts on pages
body: <p>My situation is somewhat complex, I'll try to explain it as succinctly as possible. </p>
  
  <p>I'm currently using <code>query_posts</code> to modify the main query on custom pages on my site, which as far as I can tell works quite well, though I've read that using query_posts is bad practice for a number of different reasons. </p>
  
  <p>So, why am I using <code>query_posts</code> and not creating a <code>WP_Query</code> object you may ask? </p>
  
  <p>It's because I'm using the infinite-scroll plugin, infinite-scroll doesn't play nice with WP_query, but it works absolutely fine when you simply modify the main query with query_posts. For example, pagination doesn't work using infinite scroll + WP_query (main concern). </p>
  
  <p>On one page, I'm modifying the query to get most viewed posts.</p>
  
  <pre><code>&lt;?php $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; ?&gt;     
  &lt;?php query_posts( array( 'meta_key' =&gt; 'wpb_post_views_count', 'orderby' =&gt; 'meta_value_num', 'order' =&gt; 'DESC' ,  'paged' =&gt; $paged, ) ); ?&gt;     
  
  
  &lt;?php if (have_posts()) : ?&gt;
  
  &lt;?php while ( have_posts() ) : the_post() ?&gt;
  
      &lt;?php if ( has_post_format( 'video' )) {
              get_template_part( 'video-post' );
          }elseif ( has_post_format( 'image' )) {
              get_template_part( 'image-post' );
          } else {
             get_template_part( 'standard-post' );
          }
  
      ?&gt;
  
  &lt;?php endwhile;?&gt;
  
  &lt;?php endif; ?&gt;
  </code></pre>
  
  <p>So after a lot of reading I gather that my other option to modify the main query is using <code>pre_get_posts</code>, though I'm somewhat unsure as to how to go about this.</p>
  
  <p>Take this for example:-</p>
  
  <pre><code>function textdomain_exclude_category( $query ) {
      if ( $query-&gt;is_home() &amp;&amp; $query-&gt;is_main_query() ) {
          $query-&gt;set( 'cat', '-1,-2' );
      }
  }
  add_action( 'pre_get_posts', 'textdomain_exclude_category' );
  </code></pre>
  
  <p>Alright, so simple enough - if it's the home page, modify the main query and exclude two categories. </p>
  
  <p>What I'm confused about and can't figure out is:-</p>
  
  <ol>
  <li><p>the use case scenario for custom page templates. With my <code>query_posts</code> modification I can just drop in the array before <code>if (have_posts())</code>, select my page template, publish it and away I go. 
  With <code>pre_get_posts</code> I can't figure out how to say for example <code>$query-&gt;most-viewed</code> etc</p></li>
  <li><p><code>array( 'meta_key' =&gt; 'wpb_post_views_count', 'orderby' =&gt; 'meta_value_num', 'order' =&gt; 'DESC' ,  'paged' =&gt; $paged, ) );</code></p></li>
  </ol>
  
  <p>How the heck do I do that with <code>pre_get_posts</code> and make sure it's paginated, ie. works with infinite scroll? In all the examples I've seen with <code>pre_get_posts</code> there's no arrays. </p>