WordPress.org

Ready to get started?Download WordPress

Forums

$found_posts issues when using WP_Query with meta_compare (10 posts)

  1. lherry
    Member
    Posted 1 year ago #

    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 !

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    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?

  3. lherry
    Member
    Posted 1 year ago #

    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...

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Can you post the full template file code, see the Forum Welcome on how to post code.

    Is this a custom Page template or a Page template?

    What does getCurrentCatID() return?

  5. lherry
    Member
    Posted 1 year ago #

    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

    Thanks !

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this after the wp-paginate function:

    <?php wp_reset_postdata(); wp_reset_query(); ?>
  7. lherry
    Member
    Posted 1 year ago #

    It does not change anything : 4 posts, and 4 pages available.

  8. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this pastebin: http://pastebin.com/g22kw035
    Be careful for typos because I haven't tested this on my own site.

  9. lherry
    Member
    Posted 1 year ago #

    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 !

  10. keesiemeijer
    moderator
    Posted 1 year ago #

    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://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

    I'm glad you've got it resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic