Make WordPress Core

Opened 13 years ago

Closed 9 years ago

Last modified 5 years ago

#21596 closed enhancement (fixed)

enhance wp_get_archives to support post types

Reported by: jjminkle's profile jjminkle Owned by: wonderboymusic's profile wonderboymusic
Milestone: 4.4 Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch
Focuses: Cc:

Description

I needed to populate an option list of a form with the titles of custom post types. It seemed like wp_get_archives would do the trick, except there was no way to tell it to grab a post_type. So I enhanced wp_get_archives to support and additional parameter.

Attachments (2)

wp_get_archives.php.patch (755 bytes) - added by jjminkle 13 years ago.
svn diff
21596.diff (3.1 KB) - added by wonderboymusic 9 years ago.

Download all attachments as: .zip

Change History (15)

@jjminkle
13 years ago

svn diff

#1 @SergeyBiryukov
13 years ago

  • Keywords has-patch added

Related/duplicate: #13462

#2 @daitoku
12 years ago

This does not get the complete job done but might be a start. I think we really do need to be able to use custom post types with wp_get_archives.

#3 @DrewAPicture
12 years ago

  • Cc xoodrew@… added

#4 follow-up: @ancawonka
12 years ago

I think this would be very helpful, and would especially be nice for creating 404 pages and other indexes.

In the meantime, you can achieve the same effect by adding a posts_where filter before you do your wp_get_archives call, and then removing it right after.

#5 in reply to: ↑ 4 ; follow-up: @SergeyBiryukov
12 years ago

Replying to ancawonka:

In the meantime, you can achieve the same effect by adding a posts_where filter before you do your wp_get_archives call, and then removing it right after.

wp_get_archives() doesn't call WP_Query::get_posts(), so the posts_where filter doesn't seem applicable here.

There is, however, getarchives_where filter that can be used instead:
http://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/general-template.php#L915

#6 @iseulde
12 years ago

  • Component changed from General to Post Types

#7 in reply to: ↑ 5 @onetrev
11 years ago

Replying to SergeyBiryukov:

There is, however, getarchives_where filter that can be used instead:
http://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/general-template.php#L915

I have seen getarchives_where recommended before, but then don't users then also have to deal with permalink re-writing issues to display the archives properly and have working pagination for them? It seems a lot of casual developers and general WP users struggle with this.

So I very much agree we need full archive support in wp_get_archives, especially when you consider custom taxonomies are support was added to wp list categories.

#8 @jacklenox
11 years ago

Just so folks know, I'm working on a patch for this. I'm trying to also tidy the function up quite a bit, while of course keeping backwards compatibility.

#9 @jacklenox
11 years ago

Argh, so I've looked into this and it seems to be a fairly complex issue.

The first part of the problem is quite straightforward. As above, we can allow the wp_get_archives() function to accept post_type as an argument.

However, dealing with the URLs without getting a bit hacky seems difficult. It is of course fairly straightforward to use the rewrite_rules_array filter to handle some custom URLs but I get the impression this isn't best practice for core.

Bearing that in mind, it looks as though we would need to rewrite the get_date_permastruct method of the WP_Rewrite class so that it can also handle a custom post type. Or we need to combine that method with a new one. I don't really know enough about the class to have a valid opinion.

In the meantime I've created a plugin that contains a new function wp_get_archives_cpt() which is basically a clone of wp_get_archives() but with the added post_type support. The plugin also comprises what I believe is the quickest, dirtiest way of dealing with the URL rewriting. Find it here: https://github.com/jacklenox/archives-for-custom-post-types

Last edited 11 years ago by jacklenox (previous) (diff)

@wonderboymusic
9 years ago

#10 @wonderboymusic
9 years ago

  • Milestone changed from Awaiting Review to 4.4

21596.diff is a fresh approach. The archives work with post_type in the query string, which should suffice

#11 @wonderboymusic
9 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 34686:

Allow wp_get_archives() to accept post_type as an arg.

Adds unit test.

Fixes #21596.

#12 @DrewAPicture
9 years ago

In 34689:

Docs: Move the hook doc to once again directly precede the hook line for the getarchives_where filter following [34686].

See #21596.

This ticket was mentioned in Slack in #core by talldanwp. View the logs.


5 years ago

Note: See TracTickets for help on using tickets.