Skip Navigation

[Resolved] Help with filtering query by date

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to set up some links to my monthly archives. The post date should be displayed as the text link. If the User clicks the date, it should redirect them to a monthly archive for the corresponding month and year of the post date.

Solution: Use custom HTML and shortcodes to create a link tag in the following format:

<a href="[wpv-bloginfo show='url']/[wpv-post-date format='Y']/[wpv-post-date format='m']/?cat=141">{!{wpv-post-date}!}</a>

Relevant Documentation:
https://toolset.com/documentation/user-guides/views/views-shortcodes/

This support ticket is created 3 years, 7 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by Christian Cox 3 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#1763617
Screenshot_2020-09-01 Blog - Western States Horse Expo.png

Tell us what you are trying to do?
I have set up a custom monthly archive page in which I would like to filter posts by month based on the publish date of a post from a loop view. In my views field I have the published date and I would like that to link through to my archive page in which I have set up a view for the Monthly Archive (see attached screenshot). However, I can't figure out which query to put in to return the correct month based on the published date of the post. So for instance, if a post is published on August 30, 2020, and someone clicks on that it would take them to the archive page, which would show for all posts in August. If a post is published on September 1, 2020, it would take them to an archive for all the posts for September. After much trial and error, I have not been able to achieve this as I'm not clear on what parameters would produce this result.

Is there any documentation that you are following?
This one in particular: https://toolset.com/documentation/user-guides/views/filtering-views-query-by-date/, but I have also searched the forms and have not been able to find exactly what I'm looking for.

I also tried setting up a link as instructed in this exmaple, but got "Page not found" https://toolset.com/forums/topic/use-toolset-to-create-a-list-of-links-for-year-and-month-archive-pages-and-count/

Is there a similar example that we can see?

What is the link to your site?
hidden link

#1763689
Screen Shot 2020-09-01 at 3.37.32 PM.png

Hello, Toolset offers several front-end filtering options for custom fields, but post publish date is not easily supported. You have two basic options:
1. Create a custom date field and allow front-end filtering by that custom date field. This may require some custom code to copy the post publish date into the custom date field in Unix timestamp format.
2. Add a Query Filter for post date configured to support URL parameters for Year and Month. See the attached screenshot for an example of this configuration. Then insert two custom filter controls in the View using Views shortcodes, one for the Year and another for the Month. For example:

[wpv-control url_param="wpv-year" type="select" values=",2020,2019, 2018" display_values="All,2020,2019,2018"]
[wpv-control url_param="wpv-month" type="select" values=",1,2,3,4,5,6,7,8,9,10,11,12" display_values="All,January,February,March,April,May,June,July,August,September,October,November,December"]

There are some limitations to the second approach. For example, some data-specific features like "Only show available options for each input" may not work as expected. For that reason, it is recommended not to use that feature. Also, the years available in the input field are manually defined. You'll have to update the filter shortcode in 2021 to support that year.

#1763737

Thanks. I'm not entirely sure this addresses what I want to achieve. Maybe I didn't explain it correctly. What I am hoping to do is create a monthly archive view where one can click on the date when a post was created in the post snippet, that will take them to an archive page that I've created where the only posts that are displayed are the ones for that month.

So if they click on "September 1, 2020" it then loads the news archive page that I've created would display only posts for September 2020. If they click on August 31, 2020 they would only get posts for August 2020.

I'm not trying to make a dropdown or a list showing all the months. I already have built-in functions that are doing that.

Does that make sense?

#1763739

There's nothing built-in that would achieve this, but I think you could do something similar with custom HTML. Could you explain further:
- Are the monthly archives already set up? If so, what is the URL structure of your monthly archives? Please provide some examples of the monthly archive URLs so I know how to set up the link markup.
- How are you displaying the date in the "post snippet" you mentioned? Please provide any code or shortcodes you're using, and I'll see how we can integrate with that to produce the proper link structure.

#1763893

Yes, the monthly archives were already set up so I was able to figure something out using the WordPress structure for existing monthly archive pages. I used this and it seems to be working exactly as I had hoped:
<a href="[wpv-bloginfo show='url']/[wpv-post-date format='Y']/[wpv-post-date format='m']/?cat=141">{!{wpv-post-date}!}</a>

Thanks so much!

#1763897

My issue is resolved now. Thank you!

#1764303

Okay great, this looks like what I had in mind as well. Feel free to reach out if you have additional questions.

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