Make WordPress Core

Opened 17 months ago

Closed 7 months ago

Last modified 7 months ago

#63091 closed defect (bug) (invalid)

wp_get_archives() with post_type parameter generates duplicate query parameters in URLs

Reported by: childsview Owned by:
Priority: normal Milestone:
Component: Taxonomy Version: 6.7.2
Severity: normal Keywords:
Cc: Focuses:

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');

Change History (5)

#1 @childsview
17 months ago

Environment

WordPress version: 6.7.2
Theme: Custom theme
Plugins: Occurs with all plugins deactivated
Permalink structure: /%year%/%monthnum%/%day%/%post_id%/

#2 @hbhalodia
7 months ago

Hi @childsview, I am not able to reproduce this issue anymore. Could you please help test the issue again and let us know if you are facing the issue again.

Test version: WP 6.9

Thank You,

Version 0, edited 7 months ago by hbhalodia (next)

#3 @hbhalodia
7 months ago

  • Keywords needs-testing added

#4 @childsview
7 months ago

  • Resolutioninvalid
  • Status newclosed

Hi, thank you for testing and following up on this issue.

I have tested this on WP 6.9 and can confirm that the issue no longer occurs. After further investigation, I found that this was not a WordPress core bug, but rather an issue with my custom theme code.

The problem was caused by the following custom filters in my theme:

add_filter('getarchives_where', 'my_getarchives_where', 10, 2);
function my_getarchives_where($where, $r)
{
  global $my_archives_post_type;
  if (isset($r['post_type'])) {
    $my_archives_post_type = $r['post_type'];
    $where = str_replace('\'post\'', '\'' . $r['post_type'] . '\'', $where);
  } else {
    $my_archives_post_type = '';
  }
  return $where;
}

add_filter('get_archives_link', 'my_get_archives_link');
function my_get_archives_link($link_html)
{
  global $my_archives_post_type;
  $add_link = '';
  if ('' != $my_archives_post_type)
    $add_link .= '?post_type=' . $my_archives_post_type;
  $link_html = preg_replace("/href=\'(.+)\'/", "href='$1" . $add_link . "'", $link_html);

  return $link_html;
}

This issue can now be closed. Thank you for your assistance.

#5 @sabernhardt
7 months ago

  • Keywords needs-testing removed
  • Milestone Awaiting Review

Good to hear you solved it!

Note: See TracTickets for help on using tickets.