Skip Navigation

[Gelöst] Views filter date by a specific month (repeated date field)

This support ticket is created vor 5 Jahre, 9 Monate. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. 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+01:00)

This topic contains 3 Antworten, has 2 Stimmen.

Last updated by Nigel vor 5 Jahre, 8 Monate.

Assisted by: Nigel.

Author
Artikel
#956608

I have a CPT with date (repeated field) to record servicing dates for a particular car. I would like to be able to create a table much like a calendar, to be able to list the car that has been scheduled for a servicing in a particular month. A sample is such:

Car A: 1 July; 1 Oct
Car B: 1 July; 1 Nov

In Views:
July | Oct | Nov
A,B | A | B

In essence it is to filter by a specific month only, regardless the year and day.
Thank you.

#957112

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

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

Hi there

What you are describing wouldn't be possible with a single native query (WP_Query), and so isn't something that can be done directly in Views, either.

For a single query that returned posts the best you could do would be to order the post results by the date field, but you wouldn't have anything to delineate the start of a new month in your list of posts (although you might be able to write a custom shortcode to achieve that).

Your posts have a date field, and you need to make a Loop that iterates over months (so that you can then display the cars whose date corresponds to that month).

I can think of two ways to do that, but it requires more manual input.

The first is that you can make a month taxonomy, and assign a month term to each car post (manually as you edit the post).

Then you can create a View which queries that taxonomy, iterating over the month terms, and you nest a post View in the output section which will display the cars that have that month term assigned.

Or you could do something similar with post relationships, where you made a parent month post type, and assigned each car post to one of the months, then created nested Views much like you would with the taxonomy solution above.

If you are publishing car posts with Toolset Forms then it shouldn't be too difficult to automate setting the relevant month taxonomy term or parent post when submitting the post with the date field set.

#1071703

Hi Nigel,

Assuming I created a custom field "month", which i intend for it to be invisible (not editable by the user), is there anyway to fetch the month from my custom date field, and post into the "month" field? this way i could do filtering in Views with the "month" field, which would be less complex than using taxo or another post type.

Perhaps you can guide me on creating a shortcode to fetch the month of the date, and i could insert this shortcode into the "month" field on Toolset Forms.

Great thanks.

#1072524

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

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

If you are publishing the posts with Toolset Forms and you want to add a hidden custom field for the month automatically, you could do that with JS in the form itself, but I think the best way to do this would be to use the cred_save_data hook to run a little PHP that works out the month from the date field and sets the post meta.

Note that hidden custom fields have slugs that begin with underscore (e.g. '_my-hidden-field'), and to be able to use those for filtering in Views you need to go to Toolset > Settings > Front-end Content > Hidden Custom Fields and check the field in question (which will only show up once some posts have been created that use it).

The code you would need would be fairly simple, something like:

function tssupp_add_hidden_field( $post_id, $form_data ){

	$timestamp = $_POST['wpcf-some-date'];

	$month = date( 'M', $timestamp );

	update_post_meta( $post_id, '_hidden-month', $month );
	
}
add_action( 'cred_save_data', 'tssupp_add_hidden_field', 10, 2 );

Note that you would need to edit the name of the field in the form which stores the date ('wpcf-some-date'; note the 'wpcf-' prefix), the slug of the hidden field ('_hidden-month'), and you may also want to edit the format of the month string ('M' is for Jan, Feb etc.; see hidden link for details).

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.