I am using the plugin code linked below. But the order of the loop is random.


Ideally, products will be listed by the order in which they were viewed for each individual visitor.

How do you change the order so that the last viewed product is first?

  • You'd have to sort the query results ('the loop' bit) into the same order as $viewed_products, assuming that's the right order. The order is being lost in the database.
    – Rup
    Commented Mar 17, 2020 at 11:23
  • So should this work: 'orderby' => $viewed_products
    – iamonstage
    Commented Mar 17, 2020 at 13:34
  • No, sorry. If you wanted to do the ordering in the database then you'll have to put $viewed_products into a temporary table with an index and replace the 'in' clause with a join to the temporary table. You could then order on the index of the temporary table.
    – Rup
    Commented Mar 17, 2020 at 13:41
  • I am sorry, I am not a competent enough developer to do this.
    – iamonstage
    Commented Mar 17, 2020 at 14:06


Your Answer

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

Browse other questions tagged or ask your own question.