1

On my WP theme template page, I am running a new WP_Query. It seems to be calling my pre_get_posts filter twice, which then wrecks my custom parameters. The second time the pre_get_posts filter is called, my custom taxonomy query no longer exists.

Here is my call of the new WP_Query:

$wp_new_query = new WP_Query( array ( 'checkpermissions'=>true, 'post_type'=>'resources', 'custom_tax' => $tax_query, 'date_query'=>array('after'=>"-".$num_days. " days") )) ;

In my functions.php file, I have this pre_get_posts filter:

function pw_filter_query( $query ) {

  print "NEW<BR>";

  global $user_role;

  if($query->get( 'custom_tax' ))
  {
    $query->set('tax_query', $query->get( 'custom_tax' ));
    if(WP_DEBUG)
    {
        print "TAX:";print_r($query->get( 'tax_query' ));print "<br>";
    }
  }

}
add_action('pre_get_posts', 'pw_filter_query', 9999);

The output from this is:

NEW
TAX:Array ( [field] => id [taxonomy] => resource_category [terms] => Array ( [0] => Array ( [0] => 91 ) ) )
END

NEW
END

What am I missing?

I used the php debug_print_backtrace() and both times the pre_get_posts filter is called, it is triggered by the same "new WP_Query" line in the theme template file.

I also disabled all plugins, and no change. Running WP 4.2.1

  • Wrap all your code inside your function in an if ( $query->is_main_query() ) { ALL YOUR FUNCTIONS CODE } – Pieter Goosen Apr 29 '15 at 16:55
  • I don't want it to run on the main query though, because I am filtering a custom post type (not main query) to display on a page template (main query gets page content). Is there a way to tell why the pre_get_posts filter is running twice? – Mike Szostech Apr 29 '15 at 18:59

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.