Skip Navigation

[Resolved] Setup filter by year

This support ticket is created 5 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 8.94 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by pedro.S 5 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#1193832

Tell us what you are trying to do? A dropdown select to filter by year

Is there any documentation that you are following? No

Is there a similar example that we can see? No

Nigel,

To my question previously answered by you today, we decided to go to another approach regarding the WordPress Archives filter by year and month widget.

We wish to implement a filter that filters content by year, which is a select. This will be a Toolset custom field.
We believe the easiest approach is to use custom taxonomy in the filter section.

https://toolset.com/forums/topic/set-up-filter-by-year/

To avoid duplication of work, In the post above you suggested adding a save_post hook that sets the category year according to the saved date.
But would it possible to no use the saved date (which i think you are talking about the "modified" date), instead use the original published date, would this be possible? If so, is there any snippet that does this?

#1194157

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Pedro

You can use the following code snippet to automatically assign a year taxonomy term to posts when they are saved/updated based upon the original publication date.

You'll need to edit the slug of the taxonomy (in my example the taxonomy has a slug of 'published-year').

You will also need to create the terms beforehand, i.e. create the year terms 2019, 2018, 2017 etc. in the WP admin.

function tssupp_assign_year($post_id, $post, $update) {

	$tax_slug = 'published-year';

	// does post already have year taxonomy set?
	$year_term = get_the_terms($post, $tax_slug);

	if (!$year_term) {

		// get the year of the published date and set term
		$year = substr($post->post_date, 0, 4);

		$year_term = get_term_by('name', $year, $tax_slug);

		wp_set_post_terms($post_id, array($year_term->term_id), $tax_slug);

	}
}
add_action('save_post', 'tssupp_assign_year', 10, 3);
#1194381

Thanks Nigel!