Title: $found_posts issues when using WP_Query with meta_compare
Last modified: August 20, 2016

---

# $found_posts issues when using WP_Query with meta_compare

 *  [lherry](https://wordpress.org/support/users/lherry/)
 * (@lherry)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/found_posts-issues-when-using-wp_query-with-meta_compare/)
 * Hi,
 * I’m just getting started in WP experience and I meet a little problem : I’m using
   this Wp_Query :
 *     ```
       $agenda = new WP_Query( array(
                         'cat' => getCurrentCatID(),
                         'meta_key' => 'date',
                         'meta_value' => getDateWithTol(),
                         'meta_compare' => '<',
                         'orderby' => 'meta_value_num',
                         'order' => 'ASC',
                         'post_status' => 'publish',
                         'posts_per_page' => get_option('posts_per_page'),
                         'paged' => (get_query_var('paged')) ?
                                     get_query_var('paged') : 1 ));
       ```
   
 * This query return 4 posts, but the property $wp_query->found_posts returns 32
   posts which creates problem with the plugin wp_paginate which creates more pages
   than it is needed…
 * Is anybody has an idea or can hep me please ?
    Thanks a lot !

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

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/found_posts-issues-when-using-wp_query-with-meta_compare/#post-3400278)
 * Does the query return the correct (4) posts or are there more posts?
    How many
   posts do you set with `get_option('posts_per_page')`? is this on a category archive
   page?
 *  Thread Starter [lherry](https://wordpress.org/support/users/lherry/)
 * (@lherry)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/found_posts-issues-when-using-wp_query-with-meta_compare/#post-3400280)
 * No, the query return the correct posts (4).
    I’ve tuned WP to display 9 posts
   per pages so I normally have only one page, but with the $found_posts returning
   32…
 * Our template designer has desactivated the archive property so we don’t have 
   any archive page…
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/found_posts-issues-when-using-wp_query-with-meta_compare/#post-3400284)
 * Can you post the full template file code, see the [Forum Welcome](http://codex.wordpress.org/Forum_Welcome#Posting_Code)
   on how to post code.
 * Is this a custom Page template or a [Page template](http://codex.wordpress.org/Pages#Page_Templates)?
 * What does `getCurrentCatID()` return?
 *  Thread Starter [lherry](https://wordpress.org/support/users/lherry/)
 * (@lherry)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/found_posts-issues-when-using-wp_query-with-meta_compare/#post-3400286)
 * So, it is a custom Page template to display agenda posts…
 * getCurrentCatID() is a template function :
 *     ```
       function getCurrentCatID()
       {
         global $wp_query;
         $cat_ID = $wp_query->query_vars[ 'cat' ];
   
         if( !$cat_ID )
         {
           foreach(( get_the_category()) as $category ) $cat_ID = $category->cat_ID;
         }
   
         return $cat_ID;
   
       } // getCurrentCatID()
       ```
   
 * Here is the category custom template file code : [http://pastebin.com/kEHzY79C](http://pastebin.com/kEHzY79C)
 * Thanks !
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/found_posts-issues-when-using-wp_query-with-meta_compare/#post-3400297)
 * Try it with this after the wp-paginate function:
 *     ```
       <?php wp_reset_postdata(); wp_reset_query(); ?>
       ```
   
 *  Thread Starter [lherry](https://wordpress.org/support/users/lherry/)
 * (@lherry)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/found_posts-issues-when-using-wp_query-with-meta_compare/#post-3400298)
 * It does not change anything : 4 posts, and 4 pages available.
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/found_posts-issues-when-using-wp_query-with-meta_compare/#post-3400300)
 * Try it with this pastebin: [http://pastebin.com/g22kw035](http://pastebin.com/g22kw035)
   
   Be careful for typos because I haven’t tested this on my own site.
 *  Thread Starter [lherry](https://wordpress.org/support/users/lherry/)
 * (@lherry)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/found_posts-issues-when-using-wp_query-with-meta_compare/#post-3400323)
 * Thanks a lot for your help : it works !
 * It was missing the definition of global $wp_query, but it seems to work perfectly.
 * If understand well, you :
    – backup the wp_query in a temp object – initialize
   the wp_query object – do the query – exploit datas – restore the wp_query object
   to its initial state
 * Is that right ?
 * How do you explain that we need to initialize and use directly the wp_query object
   to correct the problem ?
 * Thanks a lot for the time you given to help me !
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/found_posts-issues-when-using-wp_query-with-meta_compare/#post-3400329)
 * > Is that right?
 * Yes, but…
    new WP_Query() is (normally) used for secondary loops. wp-paginate
   uses the $wp_query object to calculate the pagination. `$wp_query->query($args);`
   is essentially the same as `query_posts($args);` which is normally used to alter
   the main (paginated) loop. Very confusing, I know 🙂
 * An even better way to query posts is by hooking into pre_get_post:
    [http://www.billerickson.net/customize-the-wordpress-query/](http://www.billerickson.net/customize-the-wordpress-query/)
   [http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/](http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/)
   [http://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts](http://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts)
 * I’m glad you’ve got it resolved.

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

The topic ‘$found_posts issues when using WP_Query with meta_compare’ is closed 
to new replies.

## Tags

 * [found_posts](https://wordpress.org/support/topic-tag/found_posts/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 2 participants
 * Last reply from: [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * Last activity: [13 years, 4 months ago](https://wordpress.org/support/topic/found_posts-issues-when-using-wp_query-with-meta_compare/#post-3400329)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
