title: Why the $query->is_main_query() returning false (wordpress)
body: <p>I have a very simple index.php file in my wordpress myTheme folder where I want to show only the posts with "friends" category using the 'pre_get_posts' hook. But the $query->is_main_query() in the if condition is returning false. That's why it's not working, it's showing all the posts. Here is the code in my index.html file:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  function myCategory( $query ) {
   if ( ! is_admin() &amp;&amp; $query-&gt;is_main_query() &amp;&amp; $query-&gt;is_search() ) {
    $query-&gt;set( 'category_name', 'friends' );
   }
  }
  add_action( 'pre_get_posts', 'myCategory' );
  
  
  $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
  $wp_query = new WP_Query( 'posts_per_page=2&amp;paged='.$paged );
  if ($wp_query-&gt;have_posts()) {
    while ( $wp_query-&gt;have_posts() ) {
    $wp_query-&gt;the_post();
    echo "&lt;h2&gt;&lt;a href='".get_the_permalink()."'&gt;".get_the_title()."&lt;/a&gt;&lt;/h2&gt;";
    the_content("read more..",false);
   }
  } 
  previous_posts_link("&lt;&lt; Previous");
  next_posts_link("More &gt;&gt;");
  
  ?&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>But when I am using the following code just removing the $query->is_main_query() from the if condition inside the myCategory() function, it's working.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  function myCategory( $query ) {
   if ( $query-&gt;is_home() &amp;&amp; ! is_admin() ) {
    $query-&gt;set( 'category_name', 'friends' );
   }
  }
  add_action( 'pre_get_posts', 'myCategory' );
  
  
  $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
  $wp_query = new WP_Query( 'posts_per_page=2&amp;paged='.$paged );
  if ($wp_query-&gt;have_posts()) {
    while ( $wp_query-&gt;have_posts() ) {
    $wp_query-&gt;the_post();
    echo "&lt;h2&gt;&lt;a href='".get_the_permalink()."'&gt;".get_the_title()."&lt;/a&gt;&lt;/h2&gt;";
    the_content("read more..",false);
   }
  } 
  previous_posts_link("&lt;&lt; Previous");
  next_posts_link("More &gt;&gt;");
  
  ?&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Now, I want to know why the $query->is_main_query() is returning false. Could anyone explain please?</p>