question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do you return posts created in a date range?

See original GitHub issue

Hi all,

I am trying to implement an archives search in my blog, which requires me to return posts within a date range. I am trying the following but with no luck:

        let postQuery = this.$content('blog', { deep: true })
          .only(['dir', 'title', 'author', 'description', 'image', 'createdAt'])
          .sortBy('createdAt', 'desc');

        if (this.dateFilter) {
          const startDate = new Date(this.dateFilter.setDate(1));
          const endDate = new Date(this.dateFilter.setMonth(this.dateFilter.getMonth() + 1, 1));
          postQuery = postQuery.where({ createdAt: { $between: [startDate, endDate] } });
        }

        this.posts = await postQuery.fetch();

the above gives no errors, but just doesn’t return anything. I have tried all sorts of variations on this, such as:

          postQuery = postQuery.where({
            $and: [
              { createdAt: { $gte: startDate } },
              { createdAt: { $lte: endDate } }
            ]
          });

but still no luck. The only thing I can’t think of is that createdAt is not a date?

Any help much appreciated!

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
azrikaharcommented, Sep 1, 2020

I cracked the code! (and probably my head as well)

CodeSandBox Link: https://codesandbox.io/s/solitary-tree-sf9ew?file=/pages/blog/index.vue

As I mentioned the typeof thing and the using Epoch in the library, it dawned on me on why not trying to use epoch on our frontend code? Seems like it actually works! We just need to use new Date(date).valueOf() instead of the typical new Date(date) and we will have a working filter. Setting both of their value to empty string also seems to work without needing to cater cases like one of it is filled but the other not filled and might interfere with the $and query. But no, if one of them is empty, it will act as if it is not bound and we can just set startDate or endDate only. Hope it’s not just a “work in my machine/env” type of deal here.

2reactions
gsjencommented, Jan 22, 2021

valueOf() only worked for me in development. In production I had to switch to toJSON().

Read more comments on GitHub >

github_iconTop Results From Across the Web

php - Wordpress - get posts by date range - Stack Overflow
Save this question. Show activity on this post. Im trying to get a list of posts from WordPress for the last 7,30 and...
Read more >
How to use the Date Range extension to display Facebook ...
To enable the Date Range, go to the Edit settings for the feed you want to apply the filter for. Go to Edit...
Read more >
How to get posts published between a date and today?
Return posts from the last 30 days:​​ date( 'Y-m-d', strtotime( '-30 days' ) ) . "'"; return $where; } add_filter( 'posts_where', 'filter_where' ...
Read more >
Programmatically Search WordPress Posts By Date Range
Search Posts By Date Range · Register a callback with the posts_where filter, · Make sure the function accepts the string for where...
Read more >
How to Write a SQL Query For a Specific Date Range and ...
SELECT * FROM TABLE_NAME WHERE DATE_TIME_COLUMN BETWEEN 'STARTING_DATE_TIME' AND 'ENDING_DATE_TIME';. Step 1: Create a Database. For this use ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found