[Resuelto] Views filter date by a specific month (repeated date field)
This support ticket is created hace 6 años, 4 meses. 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.
Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.
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.
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.
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.
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:
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 enlace oculto for details).