• Resolved charlie67p

    (@charlie67p)


    Hi,

    What is the best way to add a specific Archive Page that display all my “projects” CPTs in descending chronological order ?

    I created a Custom Taxonomy named “year” with values 2017,2018,2019…

    But don’t find the way to display it in chronological order.

    Should I use a Filter in my functions.php ? Which one ?

    [ I’m using twenty twenty three Theme on WP 6.1.1.]

    Thanks !

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    All posts have a created and modified date associated with them, so it’s redundant to have a year associated taxonomy. You could query for posts within a specific year without any taxonomy.

    Full disclosure: I really dislike redundant data. IMO it must always be avoided. But it’s your site, do as you wish.

    An archive listing of posts should by default be in reverse chronological order. The list isn’t grouped under year headings, but they are in the right order. If you want year headings, the code involved in outputting posts in a loop could be modified to output a heading any time the current post’s year differs from that of the last post.

    In any case, if the post ordering by default is not as you want, you can alter the ordering criteria through the “pre_get_posts” action hook.

    Thread Starter charlie67p

    (@charlie67p)

    Thanks, I will check the pre_get_posts Hook.

    To be more clear about what I want to achieve :

    I try to build a website for an architecture agency.
    – Classic posts are dedicated to the News of the agency, on the homepage, here it’s OK, the date of publication of the Post does the job.
    – I made also a Custom post type “projects” that is dedicated to the houses made by the agency. Each house has a year of reference (sometimes it has 2 years of reference : Year of start of building + Year of end of building). So when we click on “Projects”, the houses should appear in chronological order.

    Moderator bcworkz

    (@bcworkz)

    Ah, I see, date of the physical project, not date of the project post type. You might reconsider using taxonomy terms to manage project dates. It’s much easier to order posts by meta values (aka custom fields) than it is term names. If you look at the “orderby” doc of possible args, you’ll see you can use “meta_value_num” but there is no equivalent “tax_term_name” or similar.

    If you really want to order by term name, you’d need to create a custom SQL query. If it’s composed properly, you could replace the WP_Query’s SQL with your own version through the “posts_request” filter. “Composed properly” means your query must return an array of matching posts just as the original WP_Query SQL would have done.

    If you do decide to organize project dates as meta values, you can set an appropriate “orderby” arg through the “pre_get_posts” action like we’ve already discussed.

    A third option is feasible — edit each post’s date to reflect that of the actual date of the project. It’s common for building projects to span multiple years, yet a date field can only reflect one date. Unless you start tracking both beginning and completion dates, you’ll need to decide which date to represent. Logical dates IMO would be either breaking ground or substantial completion. Probably doesn’t matter which, as long as it’s consistent.

    What to do with projects that are in progress or never even started? You can assign non-numeric values even though a year number is expected. Related posts will either end up sorted first or last, depending on what sorting algorithm is used.

    Thread Starter charlie67p

    (@charlie67p)

    thanks @bcworkz !

    You were right. Using ‘year’ as a taxonomy was a bad idea.

    I finally used ‘year’ as a meta value and edited my functions.php with the pre_get_posts hook. Works perfectly.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Create an Archive Page based on chronological Year Taxonomy?’ is closed to new replies.