admin管理员组

文章数量:1126140

I am using the_posts_pagination() on my native archive pages and paginate_links() on some pages with custom queries.

However, the function seems to pass on any url parameters from the current page. If I add ?xyz=123 manually in the url bar of my page, it will be passed on to the links in the pagination (it becomes permalink/page/d/?xyz=123). I'd like to have control of my output and only have it to show current permalink + page/d/.

On my archive pages I use the_posts_pagination() plain with no parameters, so this seems to be the default behaviour but I can't find any argument that turns it off?

I am using the_posts_pagination() on my native archive pages and paginate_links() on some pages with custom queries.

However, the function seems to pass on any url parameters from the current page. If I add ?xyz=123 manually in the url bar of my page, it will be passed on to the links in the pagination (it becomes permalink/page/d/?xyz=123). I'd like to have control of my output and only have it to show current permalink + page/d/.

On my archive pages I use the_posts_pagination() plain with no parameters, so this seems to be the default behaviour but I can't find any argument that turns it off?

Share Improve this question edited Feb 2, 2024 at 20:06 Tony Djukic 2,2594 gold badges18 silver badges33 bronze badges asked Jan 25, 2024 at 8:38 vividog3873vividog3873 1 6
  • that function is just a wrapper around get_the_posts_pagination, which itself uses paginate_links internally. Note that what you're asking for will cripple ugly permalinks, and may also break some archives, e.g. pagination on searches would be broken by this because the search term would be stripped out when going to page 2. What is the URL parameter you're trying to strip out? Or is this a theoretical concern you're trying to pre-empt? – Tom J Nowell Commented Jan 25, 2024 at 8:58
  • Yes i know they're both using paginate_links. I don't want to cripple anything i just don't want random user inputted query strings to be added to my links. Like can't we restrict it to only use wp parameters or the ones i specify? I dont want the user to be able to input /page/2/?uglyword=uglyword and then ?ugglyword=ugglyword is applied to the links in the pagination Although the paginate_links seems to escape everything, i still dont want this behaviour (for peace of mind mostly) – vividog3873 Commented Jan 25, 2024 at 9:17
  • still looking at it, there's a filter inside paginate_links, and that function returns an array, it's a different function that then creates the links from it. The URL args are in those arrays – Tom J Nowell Commented Jan 25, 2024 at 11:19
  • So, there is no argument i can use in the function calls to disable this behaviour? – vividog3873 Commented Jan 25, 2024 at 13:34
  • I didn't say that, I don't have the time right this moment to dig through things, so I left my research as comments in hopes that when someone attempts to write an answer it saves time ( or you use it to discover the answer before someone else does ). At the time of writing no answers have been written for your question, and that includes answers that say it is or isn't possible – Tom J Nowell Commented Jan 25, 2024 at 13:53
 |  Show 1 more comment

1 Answer 1

Reset to default 0

To address your concern about controlling the URL parameters in pagination links in WordPress, it's true that both the_posts_pagination() and paginate_links() functions can inherit existing query parameters from the current URL. This behavior is designed to maintain the context of the current query, including any filters or search terms, across paginated pages.

However, if you want to exclude certain query parameters or limit the parameters to only those that you specify, you can achieve this by filtering the generated links. WordPress provides a way to modify the pagination links through filters.

Here's a basic approach to filter out unwanted query parameters:

Use the paginate_links filter to modify the pagination links. Parse the URL and remove or manipulate the query parameters as needed. Here's an example of how you might implement this:

function wpb_filter_pagination_links($link) {
    // Parse the URL
    $url_parts = parse_url($link);

    // Parse the query string
    if (isset($url_parts['query'])) {
        parse_str($url_parts['query'], $query_args);

        // Remove unwanted query args
        unset($query_args['xyz']); // Replace 'xyz' with the parameter you want to exclude

        // Rebuild the query string without the unwanted parameter
        $url_parts['query'] = http_build_query($query_args);

        // Rebuild the URL
        $link = http_build_url($url_parts);
    }

    return $link;
}
add_filter('paginate_links', 'wpb_filter_pagination_links');

This code will remove the query parameter xyz from your pagination links. You can modify the unset() line to remove other parameters as needed.

Note that this approach requires careful consideration of how it might affect the functionality of your site. For example, removing query parameters related to filtering or searching could impact the user's navigation experience.

As always, test your changes in a development environment before applying them to your live site to ensure that they work as expected and do not introduce any issues.

本文标签: