{"id":2408905,"date":"2022-07-01T06:34:10","date_gmt":"2022-07-01T06:34:10","guid":{"rendered":"https:\/\/toolset.com\/?post_type=course-lesson&#038;p=2408905"},"modified":"2022-07-27T12:21:08","modified_gmt":"2022-07-27T12:21:08","slug":"filtering-toolset-views-by-dates","status":"publish","type":"course-lesson","link":"https:\/\/toolset.com\/de\/course-lesson\/filtering-toolset-views-by-dates\/","title":{"rendered":"Filtering Toolset Views by Dates"},"content":{"rendered":"\n<p class=\"lead\">Views packs a powerful query filter that lets you search posts based on the date they were published, or even the date they were modified.<\/p>\n\n\n\n<p>To add a Post Date filter:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Select the main <strong>View<\/strong> block.<\/li><li>In the right sidebar, expand the <strong>Content Selection<\/strong> section.<\/li><li>Click on the <strong>Add a filter<\/strong> button, and select the <strong>Post date <\/strong>filter.<\/li><\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-dynamic tb-image-frame-none aligncenter\" data-toolset-blocks-image=\"7418a7c0b3663be4f1936817237e5610\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/post-date-filter-1.png\"><img decoding=\"async\" width=\"932\" height=\"1024\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/post-date-filter-1-932x1024.png\" alt=\"\" class=\"wp-image-2426377\" srcset=\"https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/post-date-filter-1-932x1024.png 932w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/post-date-filter-1-273x300.png 273w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/post-date-filter-1-137x150.png 137w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/post-date-filter-1-768x844.png 768w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/post-date-filter-1-473x520.png 473w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/post-date-filter-1-300x330.png 300w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/post-date-filter-1.png 1116w\" sizes=\"auto, (max-width: 932px) 100vw, 932px\" \/><\/a><figcaption><div class=\"tb-image-caption\"><em>Filtering by post date<\/em><\/div><\/figcaption><\/figure>\n<\/div>\n\n\n<p>You can filter posts by comparing the <strong>published <\/strong>or <strong>modified date<\/strong> against one or more values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"compare-against-a-date\">Compare Against a Date<a href=\"https:\/\/toolset.com\/documentation\/user-guides\/views\/filtering-views-query-by-date\/#compare-against-a-date\"><\/a><\/h2>\n\n\n\n<p>The default state is to filter posts by their date compared to one value. You can use the options to check whether the post date is <strong>equal to<\/strong>, <strong>different from<\/strong>, <strong>before (or equal) to<\/strong> or <strong>later (or equal) to<\/strong> a given date. You will be presented with a series of options to set the date to check against, including <strong>date<\/strong>, <strong>month <\/strong>and <strong>year<\/strong>.<\/p>\n\n\n\n<p>Note that you have many options. You can set the usual <strong>year<\/strong>, <strong>month<\/strong>, <strong>day<\/strong>, <strong>hour<\/strong>, <strong>minute <\/strong>and <strong>second<\/strong>. But you can also set the <strong>day of the week<\/strong> or the <strong>day of the month<\/strong>. Watch out and do not try to get posts published on a Tuesday, April 13, 2015 because it was actually a Monday. \ud83d\ude42<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Compare Against a Group of Dates<\/h2>\n\n\n\n<p>You can also filter posts by comparing their dates against a set of values. Simply select to filter by dates <strong>in<\/strong>, <strong>not in<\/strong>, <strong>between<\/strong> or <strong>not between<\/strong> a set, and select the kind of date field to compare. For example, you can get posts published in 2012, 2013 and 2014:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-dynamic tb-image-frame-none aligncenter\" data-toolset-blocks-image=\"77359667d55643313fa555333eeb4792\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/filtering-between-two-dates.png\"><img decoding=\"async\" width=\"1024\" height=\"747\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/filtering-between-two-dates-1024x747.png\" alt=\"\" class=\"wp-image-2426387\" srcset=\"https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/filtering-between-two-dates-1024x747.png 1024w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/filtering-between-two-dates-300x219.png 300w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/filtering-between-two-dates-150x109.png 150w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/filtering-between-two-dates-768x561.png 768w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/filtering-between-two-dates-712x520.png 712w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/filtering-between-two-dates.png 1118w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption><div class=\"tb-image-caption\">Filtering by date between two values<\/div><\/figcaption><\/figure>\n<\/div>\n\n\n<p>Or you could get posts published in any day of the week but Saturday and Sunday:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-dynamic tb-image-frame-none aligncenter\" data-toolset-blocks-image=\"af64a48980261db88211a8982e5f47b2\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/filtering-days-of-the-week.png\"><img decoding=\"async\" width=\"1114\" height=\"868\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/filtering-days-of-the-week.png\" alt=\"\" class=\"wp-image-2426407\" srcset=\"https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/filtering-days-of-the-week.png 1114w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/filtering-days-of-the-week-300x234.png 300w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/filtering-days-of-the-week-1024x798.png 1024w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/filtering-days-of-the-week-150x117.png 150w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/filtering-days-of-the-week-768x598.png 768w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/filtering-days-of-the-week-667x520.png 667w\" sizes=\"auto, (max-width: 1114px) 100vw, 1114px\" \/><\/a><figcaption><div class=\"tb-image-caption\">Filtering by days of the week<\/div><\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Combine Conditions<\/h2>\n\n\n\n<p>You can set more than one condition for this date query filter. At the bottom, you can set the logical relationship between conditions: <strong>AND<\/strong> (which will return posts meeting all conditions) or <strong>OR<\/strong> (which will return posts meeting at least one of the conditions).<\/p>\n\n\n\n<p>For example, you could get posts that were published before January 1, 2015 and were published between 9:00am and 12:00 am and were also published on a weekday from Monday to Friday.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-dynamic tb-image-frame-none aligncenter\" data-toolset-blocks-image=\"7c60263e3372603d934443a83d4a1dc9\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2015\/04\/date-query-filter-combined-1.png\"><img decoding=\"async\" width=\"571\" height=\"1024\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2015\/04\/date-query-filter-combined-1-571x1024.png\" alt=\"\" class=\"wp-image-1025809\" srcset=\"https:\/\/toolset.com\/wp-content\/uploads\/2015\/04\/date-query-filter-combined-1-571x1024.png 571w, https:\/\/toolset.com\/wp-content\/uploads\/2015\/04\/date-query-filter-combined-1-84x150.png 84w, https:\/\/toolset.com\/wp-content\/uploads\/2015\/04\/date-query-filter-combined-1-167x300.png 167w, https:\/\/toolset.com\/wp-content\/uploads\/2015\/04\/date-query-filter-combined-1-290x520.png 290w, https:\/\/toolset.com\/wp-content\/uploads\/2015\/04\/date-query-filter-combined-1.png 636w\" sizes=\"auto, (max-width: 571px) 100vw, 571px\" \/><\/a><figcaption><div class=\"tb-image-caption\">Date query combination<\/div><\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">A Word on Values<\/h2>\n\n\n\n<p>When filtering by a date value, you can set a fixed date, use variable values, or pass the value as a parameter to the View.<\/p>\n\n\n\n<p>Variable values cover mainly three options: <strong>CURRENT_ONE<\/strong>, <strong>FUTURE_ONE<\/strong> and <strong>PAST_ONE<\/strong>.<\/p>\n\n\n\n<p><strong>CURRENT_ONE<\/strong> sets the value on the moment that the query is run, and is quite useful to get posts published this same year, month, or even day of the week.<\/p>\n\n\n\n<p><strong>FUTURE_ONE <\/strong>and <strong>PAST_ONE <\/strong>need a reach number. They can be used to get posts published last month, for example.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-dynamic tb-image-frame-none aligncenter\" data-toolset-blocks-image=\"46f58c895418e7fa6bce7bef288217bb\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/past-one-value.png\"><img decoding=\"async\" width=\"954\" height=\"1024\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/past-one-value-954x1024.png\" alt=\"\" class=\"wp-image-2426431\" srcset=\"https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/past-one-value-954x1024.png 954w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/past-one-value-280x300.png 280w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/past-one-value-140x150.png 140w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/past-one-value-768x824.png 768w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/past-one-value-484x520.png 484w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/past-one-value-300x322.png 300w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/past-one-value.png 1118w\" sizes=\"auto, (max-width: 954px) 100vw, 954px\" \/><\/a><figcaption><div class=\"tb-image-caption\">Getting posts published last month<\/div><\/figcaption><\/figure>\n<\/div>\n\n\n<p><strong>Example 1:<\/strong> If a <strong>PAST_ONE<\/strong> value is used for the <strong>Year<\/strong> field and the numerical value is set to <strong>2<\/strong> , then the View will display posts published two years ago (relative to the current year).<\/p>\n\n\n\n<p><strong>Example 2:<\/strong> If a <strong>FUTURE_ONE<\/strong> value is used for the <strong>Month <\/strong>field and the numerical value is set to <strong>1<\/strong>, then we are filtering for posts published in the month after the current month, regardless of the year published.You can also set the values to come from external parameters, using a shortcode attribute or a URL parameter. Using a shortcode attribute will let you reuse the same View for several different filters. You can filter by year set by a shortcode attribute <strong>filteryear<\/strong>, as shown in the following image.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-dynamic tb-image-frame-none aligncenter\" data-toolset-blocks-image=\"3806d9f18ad6f575513f19688c1798af\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/shortcode-attribute-in-date-filter.png\"><img decoding=\"async\" width=\"952\" height=\"1024\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/shortcode-attribute-in-date-filter-952x1024.png\" alt=\"\" class=\"wp-image-2426443\" srcset=\"https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/shortcode-attribute-in-date-filter-952x1024.png 952w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/shortcode-attribute-in-date-filter-279x300.png 279w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/shortcode-attribute-in-date-filter-140x150.png 140w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/shortcode-attribute-in-date-filter-768x826.png 768w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/shortcode-attribute-in-date-filter-484x520.png 484w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/shortcode-attribute-in-date-filter-300x323.png 300w, https:\/\/toolset.com\/wp-content\/uploads\/2022\/07\/shortcode-attribute-in-date-filter.png 1116w\" sizes=\"auto, (max-width: 952px) 100vw, 952px\" \/><\/a><figcaption><div class=\"tb-image-caption\">Filtering by year using a shortcode attribute<\/div><\/figcaption><\/figure>\n<\/div>\n\n\n<p>Then, you can load your View like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[wpv-view name=\"My View\" filteryear=\"2014\"]<\/code><\/pre>\n\n\n\n<p>Here are the valid values for each date filter field:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Year<\/strong>: must have a four-digit format<\/li><li><strong>Month<\/strong>: 1 to 12<\/li><li><strong>Day<\/strong>: 1 to 31<\/li><li><strong>Hour<\/strong>: 0 to 23<\/li><li><strong>Minute<\/strong> and <strong>second<\/strong>: 0 to 59<\/li><li><strong>Week<\/strong>: 1 to 53<\/li><li><strong>Day of the year<\/strong>: 1 to 366<\/li><li><strong>Day of the week<\/strong>: 1 to 7. This also depends on your WordPress settings. If you have set the week to start on Monday, then Monday will become <strong>1<\/strong> while Sunday will be <strong>7<\/strong>. Otherwise, Sunday equals <strong>1<\/strong> while Saturday equals <strong>7<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Filtering by Dates in Custom Fields<\/h2>\n\n\n\n<p>The Types plugin allows you to add custom fields for date and time. Filtering your Views using these date custom fields is a bit different than filtering by the Post Date.<\/p>\n\n\n\n<p>The Types custom fields for date and time are stored in the database as UNIX timestamps, which are positive integers. Therefore, when inserting a query filter to test a date custom field you should use the <strong>NUMBER <\/strong>option.<\/p>\n\n\n\n<p>The following image displays an example where a filter is used to only show posts where the date determined by the custom field, <strong>Deadline<\/strong>, is today.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-dynamic tb-image-frame-none aligncenter\" data-toolset-blocks-image=\"dd0a38a105dcd6c15f4beb9b3f912154\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2018\/09\/toolset-views-date-filter-example.jpg\"><img decoding=\"async\" width=\"730\" height=\"244\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2018\/09\/toolset-views-date-filter-example.jpg\" alt=\"Date filter dialog in Views\" class=\"wp-image-1114374\" srcset=\"https:\/\/toolset.com\/wp-content\/uploads\/2018\/09\/toolset-views-date-filter-example.jpg 730w, https:\/\/toolset.com\/wp-content\/uploads\/2018\/09\/toolset-views-date-filter-example-150x50.jpg 150w, https:\/\/toolset.com\/wp-content\/uploads\/2018\/09\/toolset-views-date-filter-example-300x100.jpg 300w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><\/a><figcaption><div class=\"tb-image-caption\">Filtering by a date in a custom field<\/div><\/figcaption><\/figure>\n<\/div>\n\n\n<p>You can enter a timestamp directly, or use the following functions to generate one:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>NOW()<\/strong> (the time at the moment in milliseconds)<\/li><li><strong>TODAY()<\/strong> (time at 00:00 on today)<\/li><li><strong>FUTURE_DAY(int)<\/strong> (time at 00:00 on the following day)<\/li><li><strong>PAST_DAY(int)<\/strong> (time at 00:00 on the previous day)<\/li><li><strong>THIS_MONTH()<\/strong> (time at 00:00 on first day of this month)<\/li><li><strong>FUTURE_MONTH(int)<\/strong> (time at 00:00 on first day of future month)<\/li><li><strong>PAST_MONTH(int)<\/strong> (time at 00:00 on first day of past month)<\/li><li><strong>THIS_YEAR()<\/strong> (time at 00:00 on Jan 1st of this year)<\/li><li><strong>FUTURE_YEAR(int)<\/strong> (time at 00:00 on Jan 1st of a future year)<\/li><li><strong>PAST_YEAR(int)<\/strong> (time at 00:00 on Jan 1st of a past year)<\/li><li><strong>SECONDS_FROM_NOW(int)<\/strong> (N seconds from the moment of execution)<\/li><li><strong>MONTHS_FROM_NOW(int)<\/strong> (N months from the moment of execution, day of month is persisted)<\/li><li><strong>YEARS_FROM_NOW(int)<\/strong> (N years from the moment of execution, day and month are the same)<\/li><li><strong>DATE(dd,mm,yyyy)<\/strong> eg. DATE(21,10,2011)<\/li><\/ul>\n\n\n\n<p>For example, let\u2019s say that the current date is <em>5th of May, 2012<\/em>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>PAST_MONTH(2)<\/strong> will return <em>1st of March, 2012<\/em>.<\/li><li><strong>FUTURE_DAY(10)<\/strong> will be <em>15th of May, 2012<\/em><\/li><li><strong>YEARS_FROM_NOW(-5)<\/strong> will result in <em>5th of May, 2007<\/em>.<\/li><\/ul>\n\n\n\n<p>Some of the functions return the first day or month of a year, while other functions keep the day\/month value (you might need both depending on the project).<\/p>\n\n\n\n<p>The <strong>_FROM_NOW()<\/strong> functions accept positive or negative numbers as parameters so that you can calculate future or past dates.<\/p>\n","protected":false},"author":4341,"featured_media":2408973,"template":"","related-lesson":[8411],"class_list":["post-2408905","course-lesson","type-course-lesson","status-publish","has-post-thumbnail","hentry","related-lesson-view"],"_links":{"self":[{"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/course-lesson\/2408905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/course-lesson"}],"about":[{"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/types\/course-lesson"}],"author":[{"embeddable":true,"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/users\/4341"}],"version-history":[{"count":6,"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/course-lesson\/2408905\/revisions"}],"predecessor-version":[{"id":2426451,"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/course-lesson\/2408905\/revisions\/2426451"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/media\/2408973"}],"wp:attachment":[{"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/media?parent=2408905"}],"wp:term":[{"taxonomy":"related-lesson","embeddable":true,"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/related-lesson?post=2408905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}