0

I have the following set of Arguments I'm running in a WP_Query.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;    
$args = array(
        'tag' => $taxonomy,
        'post_type' => 'initiative',
        'paged' => $paged,
        'posts_per_page' => 9,
        'category_name' => $category,
        'tag__not_in' => 15,
        'meta_key' => 'initative_weight',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'post_status' => 'publish'
      );

As this is, it works fine but when I page through the results, I see a handful of Duplicate content. Now, if I get rid of the pagination and just show everything, no duplicates.

$args = array(
            'tag' => $taxonomy,
            'post_type' => 'initiative',
            'posts_per_page' => -1,
            'category_name' => $category,
            'tag__not_in' => 15,
            'meta_key' => 'initative_weight',
            'orderby' => 'meta_value_num',
            'order' => 'DESC',
            'post_status' => 'publish'
          );

Also, if I keep the pagination, but remove the orderby, the pagination exists and no duplicates.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;    
    $args = array(
            'tag' => $taxonomy,
            'post_type' => 'initiative',
            'paged' => $paged,
            'posts_per_page' => 9,
            'category_name' => $category,
            'tag__not_in' => 15,
            'post_status' => 'publish'
          );

So, what am I doing wrong here? I seem unable to run a WP_Query with Pagination & sorting by a custom field without getting duplicates. Yet, I can do each on it's own just fine. Am I missing a step or did I come across a bug?

4
  • I think your issue must be with using tag and tag__not_in at the same time.
    – Robert hue
    Sep 16, 2015 at 16:00
  • Please show the code for your pagination and your loop in an edit. Could easily be that you override the main query, at least that would be my assumption when I read "works fine without pagination".
    – kaiser
    Sep 16, 2015 at 16:12
  • 1
    note that tag__not_in must be an array.
    – Milo
    Sep 16, 2015 at 16:42
  • @Milo Yep, that was the issue. I put it in an array and it works. Strangely the order isn't the same (it is sorting correctly, but the values are 1-5 and most are set to 3. The ones set to 3 are different depending on if there is pagination.) Either way, that fixed it. If you want to submit and answer I'll gladly mark it as correct.
    – rmlumley
    Sep 16, 2015 at 17:46

1 Answer 1

0
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  $args = array(
    'tag' => $taxonomy,
    'post_type' => 'initiative',
    'paged' => $paged,
    'posts_per_page' => 9,
    'category_name' => $category,
    'tag__not_in' => array(15),
    'meta_key' => 'initative_weight',
    'orderby' => array('meta_value_num' => 'DESC', 'title' => 'ASC'),
    'post_status' => 'publish'
  );

I changed tag__not_in to an array and updated the orderby to be an array as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.