#21596 closed enhancement (fixed)
enhance wp_get_archives to support post types
Reported by: |
|
Owned by: |
|
---|---|---|---|
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)
Change History (15)
#2
@
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.
#4
follow-up:
↓ 5
@
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:
↓ 7
@
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
#7
in reply to:
↑ 5
@
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
@
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
@
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
#10
@
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
@
9 years ago
- Owner set to wonderboymusic
- Resolution set to fixed
- Status changed from new to closed
In 34686:
svn diff