0

I have a strange thing with my WP_Query, where isn't paged correctly.

$args=array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => $limit, // 10
    'paged' => $page, // paged param
    'order' => $order, // ASC
    'orderby' => 'meta_value_num',
    'meta_key' => 'ma_rate_percents',
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'ma_rating_up',
            'compare' => '>',
            'value' => 0,
            'type' => 'NUMERIC'
        ),
        array(
            'key' => 'ma_rating_down',
            'compare' => '>',
            'value' => 0,
            'type' => 'NUMERIC'
        )
    ),
    'fields' => 'ids'
);
$posts = new WP_Query($args);

In my example i get 412 results in $posts->found_posts and on first page i see 10 post id's. If i go to page=2 i get already 10 post id's, but with the last 2 ID's by page 1, on page 3 i get 4 ID's of page 2 and so on.

What's wrong with this Query? I'm also resetted the query with wp_reset_query() on the end, but nothing helps. Anyone an idea?

  • Can you show the logic for the limit and page variables? – Shawn Jan 21 '15 at 21:12
  • for sure, $limit = !empty($_GET['limit']) ? intval($_GET['limit']) : 10; and $page = !empty($_GET['site']) ? intval($_GET['site']) : 1;. This $_GET fires into on WP backend on a plugin page. – WPler Jan 21 '15 at 21:21
  • 1
    Your posts_per_page GET variable must be changing on different pages. What happens if you hardcode that value to 10 instead of using a variable? – jetlej Jan 22 '15 at 1:47
  • Nope, the same happends, if i put static posts_per_page = 10 into the query. – WPler Jan 22 '15 at 10:29
  • I'd recommend not using $posts, as this should be reserved for the main query - use something like $my_posts instead. But anyway, after the query try var_dump($my_posts); and look at the parameters in $my_posts->query. Also eusure that the limit and offset in the query are actually as you would expect in $my_posts->request;. If you are unsure on how to interpret the query, update your question to include it and we'll help. – David Gard Jan 22 '15 at 16:22

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.