How can I retrieve the current URL (whether homepage, archive, post type archive, category archive, etc) but always without the /page/{pagenum}/ part if it's present? So, if the real URL is:




then the return value will always be


8 Answers 8


You can get the current URL through home_url( $wp->request ).

Try the example below:

global $wp;

// get current url with query string.
$current_url =  home_url( $wp->request ); 

// get the position where '/page.. ' text start.
$pos = strpos($current_url , '/page');

// remove string from the specific postion
$finalurl = substr($current_url,0,$pos);

echo $finalurl;
  • This worked! Although it was returning nothing if there was no /page/X/ in the URL, so I modified a bit to make it universal. Thanks a lot! Commented Nov 29, 2016 at 9:44

Actually the easiest would be to use get_pagenum_link() which will return you the current URL without any /page/* paramaters.


You can also simply use it to build "Previous" and "Next" links dynamically using the 'paged' query variable:

// Get the current page number.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$first_page = '<a href="' . get_pagenum_link() . '" class="page-first"><<</a>';
$prev_page = '<a href="' . get_pagenum_link($paged - 1) . '" class="page-prev"><</a>';
$next_page = '<a href="' . get_pagenum_link($paged + 1) . ' class="page-next">></a>';

// And having a `WP_Query` object you can also build the link for the last page:
$max = $the_query->max_num_pages;
$last_page = '<a href="' . get_pagenum_link($max) . '" class="page-last">>></a>';
  • This is useful, but it also gives query params in the URL. you have to remove those separately.
    – trainoasis
    Commented Oct 5, 2020 at 6:12

Answer by Govind Kumar worked, however, it only returned the URL if /page/{pagenum}/ was present in the URL and returned nothing if not. I needed a universal solution that will always return the base URL without pagination, so I modified Govind's code a bit and wrapped into a function:

function get_nopaging_url() {

    global $wp;

    $current_url =  home_url( $wp->request );
    $position = strpos( $current_url , '/page' );
    $nopaging_url = ( $position ) ? substr( $current_url, 0, $position ) : $current_url;

    return trailingslashit( $nopaging_url );


echo get_nopaging_url();

Now, it always returns correct URL.

(This is useful if you need to implement some kind of post filters that add a param to filter posts by, let's say, a meta filed. So, even if a user sets the filter param on page X, the new filtered results will always start from the base URL, not page X and throwing 404 if there's less filtered posts.)

  • Very good. Very close. But '/page' isn't strict enough. For example, page-me.com will return a value; a value that's incorrect. Commented Jun 15, 2018 at 17:09
  • @ChiefAlchemist good point, didn't even think about the /page substring being part of the domain name (if www isn't used, of course). That is, imo, rather an edge-case. However, I'd encourage you to contribute by adding code that will cover this case as well. Commented Jun 18, 2018 at 21:27
  • Vorotbov - Will do. Please see below. fwiw, there are plenty of words with the substr 'page' in it. I don't think it's safe to presume it's an edge-case. Best to make it as tight as possible. Less worry over the long run. Commented Jun 20, 2018 at 13:25
  • True, but while I do agree with you in general, the question was a specific issue, and it was solved in a most efficient way for the particular case. If my domain does not have this substring, then I just don't need to use any regexes which are slower than simple substr (and which works fine for my case). It's not a public plugin or theme, thus I don't need to cover any and all possible edge cases. However, your addition is appreciated and hope may help some future devs. Thanks! Commented Jun 22, 2018 at 8:41

My specifics were very similar to Ihor Vorotnov's, exept that I had more than one parameter. So, starting from his answer, I modified the code to use a regular expression:

function get_nopaging_url() {
    $current_url =  $_SERVER[REQUEST_URI];

    $pattern = '/page\\/[0-9]+\\//i';
    $nopaging_url = preg_replace($pattern, '', $current_url);

    return  $nopaging_url;

If you want to remove all possible pagination combinations then use this snippet:

// remove pagination from url
$pattern = '/page(\/)*([0-9\/])*/i';
$url = preg_replace($pattern, '', $GLOBALS['wp']->request);

It will take care of


and is case insensitive, works for any pagination page number and also will remove any combination of trailing number/number/number... (tests here: https://regex101.com/r/9fQaLC/1)

If you want to get the full url with leading http(s) just add

$url = home_url($url);

In that line you can also add any custom GET parameter like so

$url = home_url($url . '?typ=test');
  • 1
    Personally, I'm always trying to avoid regexes if something can be done without them (and still stay simple enough). Also, add_query_arg() should be used for adding GET params instead of string concatenation. But the answer itself is correct and does the job, thanks. Commented Aug 8, 2019 at 20:59
function getCurrentUrlSeo() {
    global $wp;
    $current_url = home_url($wp->request);
    $pos = mb_strpos($current_url, '/page');
    $finalurl = $pos ? substr($current_url, 0, $pos) : $current_url;
    return $finalurl.'/';
  • Thanks - at first glance that's the same as Ihor's answer. Can you edit the answer to explain what you've changed and why this is better?
    – Rup
    Commented Mar 16, 2022 at 15:22
  • It's literally the same code, except manually force-appending / instead of trailingslashit() (which, by the way, will make sure only 1 slash is appened) Commented Mar 17, 2022 at 12:41

While this question was already answered here (not accepted), this snippet should do the trick: home_url(add_query_arg(NULL, NULL));.

  • I've tried this trick, doesn't work. The output still has /page/2/ appended. Commented Nov 29, 2016 at 9:38
  • Oh sorry, I understood you want to get the full URL but failed because it was always returning the URL without page/{pagenum}. Commented Nov 29, 2016 at 9:57
trait TraitURLStrip {

     * Strips off paging and query string. Returns a filter-the-results friendly URL
     * @param bool   $str_url
     * @param string $str_page
     * @return bool|string
    protected function urlStrip( $str_url = false , $str_page = 'page' ){

        if ( is_string( $str_url) ) {

            $arr_parse_url = wp_parse_url( $str_url );

            $str_path_no_page = '';
            if ( isset( $arr_parse_url['path'] ) ) {

                // if we're paging then remove that. please!
                $str_path_no_page = preg_replace( "/\/{$str_page}\/[0-9]*\/$/", "/", $arr_parse_url['path'] );


            $str_scheme_host = "{$arr_parse_url['scheme']}://{$arr_parse_url['host']}";

            return $str_scheme_host . $str_path_no_page;
        return false;
  • May I ask why do you return false if the $str_url isn't a string? If you accidentally (which should not happen btw) pass anything else, you're destroying the data. I suppose returning unmodified $str_url instead would be better. Also, you have a check for the string, but not making sure it's actually an URL that can be parsed (and if not, the following code will break - wp_parse_url may return false, null, int, string and array). Additionally, I'm not sure whether using it as a trait makes sense, not everyone is using classes everywhere (hey, it's WordPress, right?). Commented Jun 22, 2018 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.