Opened 6 weeks ago
Last modified 6 weeks ago
#63091 new defect (bug)
wp_get_archives() with post_type parameter generates duplicate query parameters in URLs
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 6.7.2 |
Component: | Taxonomy | Keywords: | |
Focuses: | Cc: |
Description
When using wp_get_archives() with a custom post_type parameter, the function generates URLs with duplicate query parameters. For example, when using:
wp_get_archives(array(
'type' => 'yearly',
'post_type' => 'magazine'
));
The generated URLs look like:
https://example.com/2023/?post_type=magazine?post_type=magazine
Instead of the expected:
https://example.com/2023/?post_type=magazine
This duplicate query parameter causes issues when trying to access these archive pages, as the URL is malformed with two question marks.
Steps to Reproduce
Create a custom post type (e.g., 'magazine')
Add several posts to this custom post type with different dates spanning multiple years
Add the following code to a template file:
wp_get_archives(array(
'type' => 'yearly',
'post_type' => 'magazine'
));
View the page and inspect the generated archive links
Expected Result
Archive links should have a single query parameter:
https://example.com/2023/?post_type=magazine
Actual Result
Archive links have duplicate query parameters:
https://example.com/2023/?post_type=magazine?post_type=magazine
Environment
WordPress version: [Your WP version]
Theme: [Your theme, but also tested with default theme]
Plugins: [List plugins, or note that it happens with all plugins deactivated]
Permalink structure: [Your permalink structure]
Workaround
Currently using a filter to fix the URLs:
function fix_duplicate_post_type_query($link_html) {
return preg_replace('/?post_type=([^&]+)?post_type=\1/', '?post_type=$1', $link_html);
}
add_filter('get_archives_link', 'fix_duplicate_post_type_query');
Environment
WordPress version: 6.7.2
Theme: Custom theme
Plugins: Occurs with all plugins deactivated
Permalink structure: /%year%/%monthnum%/%day%/%post_id%/