I am trying to:
Create a simple dropdown sidebar filter that will list the years of my custom post so the viewer can select a year and have only the publications for that year be displayed.
I've tried to create a parametric search that will populate the dropdown with year from the publication-date using the following:
[wpv-control taxonomy="focus" url_param="wpv-focus" type="select" hide_empty="true" format="Y" default_label="Select a Dimension"]
and then in my layout, here is my loop:
<wpv-loop>
[types field="publication-date" style="text" format="Y"][/types]
</wpv-loop>
But this gives me a blank dropdown. If I use type="date", i get the calendar, but no way to just select a year.
There's got to be a simple way to do this and I just can't wrap my brain around it right now. Any suggestions?
I assume the "publication-date" field is a custom date field created with Types, it is not possible within Views filter form, the format attribute of shortcode wpv-control do not support year "Y" as value, see our document:
https://toolset.com/documentation/views-shortcodes/#wpv-control
format:
'%%COUNT%% %%NAME%%'
optional - %%COUNT% will display the number of post for the taxonomy term and %%NAME%, the taxonomy term. e.g. format=’%%NAME%% (%%COUNT%%)’
I suggest you create a custom field "year" to store the year value of each post, then setup the dropdown in the filter form as what you need.
I'd like to avoid creating another field for year. Is there a simple javascript I can add to extract the year from the date and use that as a variable in the filter? Or use a range - for example, find all records in 2004 by suing between 1/1/2004 - 12/31/2004. Either way, I can manually add the dropdown list since it won't contain many years, I just need a way to compare against the publication date and only select the records for that year.
That was a typo - I meant to say "by using between 1/1/2004 - 12/31/2004." Not suing anyone!
Sorry, there isn't such simple javascript, you can try Check out our certified partners for it:
https://toolset.com/consultant/
Or I you can try manually create the "year" dropdown, and use views filter hook wpv_filter_query to apply the timestamp range into query:
wpv_filter_query
When displaying a View listing posts, this filter is applied to the arguments being generated by the View settings before they are passed to the WP_Query class.
https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/