<p>First thing I recommend is to read through the <a href="http://buildwithcraft.com/docs/templating/search-form" rel="nofollow">Search Form</a> guide from the Craft documentation.
To explain the rest I'll add some comments to the example code.</p>

<p><em>form.html</em></p>

<pre><code>{# Use the `group` filter to get the years, entries were posted in #}
{% set entries = craft.entries.section('news') %}
{% set entriesByYear = entries|group('postDate|date("Y")') %}

&lt;select name="year"&gt;

    {# Go through the `year` groups #}
    {% for year, entries in entriesByYear %}
        &lt;option value="{{ year }}"&gt;{{ year }}&lt;/option&gt;
    {% endfor %}

&lt;/select&gt;
</code></pre>

<p><em>results.html</em></p>

<pre><code>{# Get the params #}
{% set year = craft.request.getParam('year') %}

{# Compose a param in this format: 'and, &gt;= 2014-01-01, &lt; 2015-01-01' #}
{% set dateParam = 'and, &gt;= ' ~ year ~ '-01-01, &lt; ' ~ (year + 1) ~ '-01-01' %}

{# Query and loop through the entries #}
{% set entries = craft.entries.section('news').postDate(dateParam) %}

{% if entries|length %}
    ...
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-03-03T10:15:11.610Z
Ok, How does this make Ajax, as in the admin panel)
Vitaliy  Badalov
CC BY-SA 3.0
2015-03-03T10:24:55.073Z
First make everything work as you expect it to and then implement the JS. How to do that exactly would be another question, but basically you need to prevent the full page reload (`event.preventDefault()` method) and then load in the newly filtered content. There's other great answers on Ajax topics here on Craft SE (→ i.e. [these ones](http://craftcms.stackexchange.com/q/4809/125) here).
carlcs
CC BY-SA 3.0
2015-03-12T11:25:46.803Z
As instead of `'postDate | date ("Y")'` substitute custom fields?
Vitaliy  Badalov
CC BY-SA 3.0
2015-03-12T12:18:57.440Z
This should actually work the same for any custom Date/Time fields, just replace the field handle like so `'myCustomDateField|date("Y")'`.
carlcs
CC BY-SA 3.0
2015-03-03T09:02:36.230Z
2015-03-03T09:02:36.230Z
carlcs
<p>For example have the following code:</p>

<pre><code>&lt;form action=""&gt;
       &lt;select&gt;
               &lt;option&gt;2015&lt;/option&gt;
               &lt;option&gt;2014&lt;/option&gt;
               &lt;option&gt;2013&lt;/option&gt;
               &lt;option&gt;2012&lt;/option&gt;
      &lt;/select&gt;

      &lt;input type="submit" value="select"&gt;
&lt;/form&gt;
</code></pre>

<p>how to get a entries of the selected year?</p>

<templating><entries><form><datetime>
Form select entries by year
# Form select entries by year

(Asked by: username_0 on 2015-03-03)

For example have the following code:

       
               2015
               2014
               2013
               2012
      

      


how to get a entries of the selected year?

**Tags:** templating, entries, form, datetime

---

## 1 answers

### Answer by username_1 on 2015-03-03. Score: 2

First thing I recommend is to read through the Search Form guide from the Craft documentation.
To explain the rest I'll add some comments to the example code.
form.html
{# Use the `group` filter to get the years, entries were posted in #}
{% set entries = craft.entries.section('news') %}
{% set entriesByYear = entries|group('postDate|date("Y")') %}



    {# Go through the `year` groups #}
    {% for year, entries in entriesByYear %}
        {{ year }}
    {% endfor %}



results.html
{# Get the params #}
{% set year = craft.request.getParam('year') %}

{# Compose a param in this format: 'and, >= 2014-01-01, < 2015-01-01' #}
{% set dateParam = 'and, >= ' ~ year ~ '-01-01, < ' ~ (year + 1) ~ '-01-01' %}

{# Query and loop through the entries #}
{% set entries = craft.entries.section('news').postDate(dateParam) %}

{% if entries|length %}
    ...
{% endif %}

> **Comments:**
> **username_0**: Ok, How does this make Ajax, as in the admin panel)  
> **username_1**: First make everything work as you expect it to and then implement the JS. How to do that exactly would be another question, but basically you need to prevent the full page reload (`event.preventDefault()` method) and then load in the newly filtered content. There's other great answers on Ajax topics here on Craft SE (→ i.e. [these ones](http://craftcms.stackexchange.com/q/4809/125) here).  
> **username_0**: As instead of `'postDate | date ("Y")'` substitute custom fields?  
> **username_1**: This should actually work the same for any custom Date/Time fields, just replace the field handle like so `'myCustomDateField|date("Y")'`.  

---

