How do you return posts created in a date range?
See original GitHub issueHi 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:
- Created 3 years ago
- Comments:14 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 typicalnew 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.valueOf()
only worked for me in development. In production I had to switch totoJSON()
.