• Hi, I have been looking around for a solution, but have a hard time even knowing what to google to find answers.

    What i want:

    I have a wordpress template that lists all the content of a custom post type called albums

    all these albums have a field that contains their release date.

    now i’d like a selectbox on that same template to make the loop only show the albums released in the selected year. i.e all albums from 2012.

    Is there a way to extend the permalink site.com/albums, to site.com/albums/2012, and make that filter the loop to only show the albums with that release date?

    my custom field is called release-date.

    in other words can i get that permalink /year become a variable that i can use in the template? or is there a better approach to get that same result?

    If anyone can point me to articles or tutorials about this, or even suggest an approach, that would be very helpful.

    Thank you so much!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You can always do something like this with the Rewrite API. For year URL fragments, the request parser is pretty good at picking these up, so you may not need to explicitly create a rewrite rule. Try hooking the ‘pre_get_posts’ action and var_dump() the query object after making your desired request. This will show you how the request parser interpreted your request on its own. There’s a good chance it picked up the year and it’s available as a query variable without a rewrite.

    The problem you’ll have is the query parser will try to find posts published in the specified year. You will need to cause this query variable to be applied to the release date added field instead of the publish date. This done via the same action, get the year value and use it to setup a meta_query. Then set the year query variable to an empty string to prevent a query by date published.

    The other issue is the release date is probably stored as a string, not as a date stamp, so the usual date functions cannot be used. Fortunately, if you only need to query for a particular year, a simple string match may suffice as long as all year data is the full 4 digits. Your meta_query ‘compare’ parameter should be ‘LIKE’ to find the year string in a longer string.

Viewing 1 replies (of 1 total)
  • The topic ‘How do I filter the WordPress loop with a permalink variable’ is closed to new replies.