Title: pre_get_posts exclude widgets query
Last modified: October 8, 2018

---

# pre_get_posts exclude widgets query

 *  Resolved [djdavedawson](https://wordpress.org/support/users/djdavedawson/)
 * (@djdavedawson)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/pre_get_posts-exclude-widgets-query/)
 * Hello, I am using the following to modify the number of posts on this template
   page. However, it is also changing the Recent posts widget. How can I exclude
   the widgets from being affected.
 *     ```
       function team_pagesize( $query ) {
   
       	if (is_page_template('team_list.php') ) {
   
               $query->set( 'posts_per_page', 50 );
       		$query->set( 'order', 'ASC' );
               return;
           }
       }
   
       add_action( 'pre_get_posts', 'team_pagesize', 1 );
       ```
   
 * Thanks in Advance
 * ~D

Viewing 8 replies - 1 through 8 (of 8 total)

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [7 years, 8 months ago](https://wordpress.org/support/topic/pre_get_posts-exclude-widgets-query/#post-10761634)
 * add a test for is_main_query as well as the page template
 * [https://codex.wordpress.org/Function_Reference/is_main_query](https://codex.wordpress.org/Function_Reference/is_main_query)
 *  Thread Starter [djdavedawson](https://wordpress.org/support/users/djdavedawson/)
 * (@djdavedawson)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/pre_get_posts-exclude-widgets-query/#post-10761660)
 * Steven,
 * Sorry for the confusion. Actually it appears that the widget in question is a
   custom one that came with the theme. This widget calls the query like this ->
 * `$the_query = new WP_Query( $args );`
 * Which is the same way the page template does. Although the $args are different
   and call different post types. I can seem to use the post type to write a conditional
   statement.
 *  Thread Starter [djdavedawson](https://wordpress.org/support/users/djdavedawson/)
 * (@djdavedawson)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/pre_get_posts-exclude-widgets-query/#post-10761728)
 * I tried using this condition is_post_type(‘team’) like this to isolate the query
   that needs changed:
 *     ```
       function team_pagesize( $query ) {
   
       	if (is_page_template('team_list.php') && is_post_type('team') ) {
   
               $query->set( 'posts_per_page', 50 );
       		$query->set( 'order', 'ASC' );
               return;
           }
       }
   
       add_action( 'pre_get_posts', 'team_pagesize', 1 ); 
       ```
   
 * and adding this below:
 *     ```
       function is_post_type($type){
           global $wp_query;
           if($type == get_post_type($wp_query->post->ID)) return true;
           return false;
       }
       ```
   
 * But it won’t recognize the post type from the $args on the page template.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/pre_get_posts-exclude-widgets-query/#post-10762091)
 * Be sure that you are clear that is_page_template() relates to the main query,
   which may or may not be the query passed to team_pagesize(). Also the query does
   not yet know the results of the query, it only knows what parameters are going
   to be used. Thus $wp_query->post is invalid, there is no such object. You can
   determine the post type being queried from $wp_query->query_vars->post_type, 
   which may be a string or array of post types.
 * If you var_dump $query->query_vars for each query on the page, you should be 
   able to identify values that distinguish one query from another.
 *  Thread Starter [djdavedawson](https://wordpress.org/support/users/djdavedawson/)
 * (@djdavedawson)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/pre_get_posts-exclude-widgets-query/#post-10765995)
 * Ok, so I did a var dump and got the team page specified -> object(WP_Query)#1503
   and the widget specified -> object(WP_Query)#2502. Not sure if those are the 
   values im looking for. I know the post types, they show in the dump also.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/pre_get_posts-exclude-widgets-query/#post-10767870)
 * What you are looking for is unique query var values that distinguish the query
   you need to affect from all other queries. If the post type is unique to that
   one query, then that’s all you need. Or maybe it narrows down applicable queries
   to two. That’s a good start. There should still be yet another query var that’s
   unique, even if it occurs in other queries already filtered out by post type.
   The combination of post type and this other query var will uniquely identify 
   the query. Besides query vars, you can make use of the various is_* properties,
   like is_single, is_archive, etc.
 * Combine all the unique criteria into a conditional statement, for example:
    `
   if ('team'== $query->query_vars->post_type && 55 == $query->query_vars->cat &&!
   $query->is_home ):` Just an example, it’s not going to work for your situation,
   though post type team of course would also be part of your actual conditional.
 * Unfortunately, you cannot make use of the object IDs seen in var_dumps (#1503,#
   2502). They are internal PHP references that are not part of the object properties
   accessible to your code. But WP IDs like $query->query_vars->page_id, if defined,
   can be used.
 *  Thread Starter [djdavedawson](https://wordpress.org/support/users/djdavedawson/)
 * (@djdavedawson)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/pre_get_posts-exclude-widgets-query/#post-10769645)
 * Ok I got it, had to set it up with brackets like this
 * `$query->query_vars['post_type']`
 * Thanks again BC !!
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/pre_get_posts-exclude-widgets-query/#post-10771069)
 * Yes! Sorry for my confusion. My on going issue of keeping arrays and objects 
   straight.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘pre_get_posts exclude widgets query’ is closed to new replies.

## Tags

 * [pre_get_posts](https://wordpress.org/support/topic-tag/pre_get_posts/)
 * [widgets](https://wordpress.org/support/topic-tag/widgets/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 8 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [7 years, 7 months ago](https://wordpress.org/support/topic/pre_get_posts-exclude-widgets-query/#post-10771069)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
