title: Why is it not possible to orderBy on different fields in Cloud Firestore and how can I work around it?
body: <p>I have a collection in firebase cloud firestore called 'posts' and I want to show the most liked posts in the last 24h on my web app.
  The post documents have a field called 'like_count' (number) and another field called 'time_posted' (timestamp).
  I also want to be able to limit the results to apply pagination.</p>
  <p>I tried to apply a filter to only get the posts posted in the last 24 hours and then ordering them by the 'like_count' and then the 'time_posted' since I want the posts with the most likes to appear first.</p>
  <pre class="lang-js prettyprint-override"><code>postsRef.where(&quot;time_posted&quot;, &quot;&gt;&quot;, twentyFourHoursAgo)
          .orderBy(&quot;like_count&quot;, &quot;desc&quot;)
          .orderBy(&quot;time_posted&quot;, &quot;desc&quot;)
          .limit(10)
  </code></pre>
  <p>However, I quickly found out that it is not possible to filter and then sort by two different fields.
  (See the Limitations part of the documentation for <a href="https://firebase.google.com/docs/firestore/query-data/order-limit-data#limitations" rel="nofollow noreferrer">Order and limit data with Cloud Firestore</a>)</p>
  <p>It states:</p>
  <blockquote>
  <p><strong>Invalid</strong>: Range filter and first orderBy on different fields</p>
  </blockquote>
  <p>I thought about sorting the results by 'like_count' in the frontend, but this won't work properly because I don't have all the documents. And getting all the documents is infeasible for a large number of daily posts.</p>
  <p>Is there an easy work-around I am missing or how can I go about this?</p>