0

Edit: Answered below

Here are my args when returning posts from a custom taxonomy:

$args = array(
    'post_type' => 'products',
    'orderby' => 'date',
    'order' => 'DESC',
    'posts_per_page' => 12,
    'tax_query' => array(
        array(
            'taxonomy' => 'product_category',
            'field' => 'slug',
            'terms' => $category, //This is pulled from a custom field
        ),
    ),
);

For some reason the posts returned are random and in random order. I've been able to fix this by setting posts_per_page to -1, but this wont work for what we are trying to do. I've also been able to fix it by breaking the while loop after creating 12 post items using a counter, but I'd like a better solution and to understand why this is happening. Anyone have any idea why? I've looked around, but the only posts I've been able to find on randomness are people wanting randomness.

Edit: Added 'order' => 'DESC', still shows random posts after the first one

  • 1
    Add 'order' => 'DESC' in query args. It will show latest 12 products. – Chinmoy Kumar Paul Aug 23 '16 at 16:31
  • @ChinmoyKumarPaul I just added that and it still shows random posts, although now it shows randomly after the first one. Weird. Step in the right direction though, so thank you! Any idea why the posts after the first one are still random? – James Hamann Aug 23 '16 at 16:43
  • @ChinmoyKumarPaulI figured it out answer is below. Thanks for your help you set me on the right track – James Hamann Aug 23 '16 at 17:33
0

I figured this out. It's because my posts were added with a CSV import which created a ton of posts at once with the exact same timestamp. Changing the orderby to something besides 'date' will suffice for now and I will need to modify my CSV import to add minutes onto the timestamp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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