1

I got it to work with this:

$url = home_url( add_query_arg( array(), $wp->request ) );

However, if the permalink is plain, all I'm getting is the homepage url (instead of the post url).

So what's the best way to get the current post or page address link?

Jacob Peattie
  • 44,319
  • 10
  • 50
  • 64
mrKC.988
  • 107
  • 2
  • 14

3 Answers3

2

Is there a reason why get_permalink() doesn't work for you? I'm unclear why you are trying to construct it manually.

Derek Held
  • 89
  • 7
0

Don't use get_permalink(). That function is intended for use inside the loop, and only works for getting the link to posts. It will not work for taxonomy archives, date archives, search results, or the latest posts page.

Normally you can get the URL to the current page with home_url() combined with $wp->request:

global $wp;
echo home_url( $wp->request );

This is because home_url() gets your site URL from the database, while $wp->request holds the path for the current request, which will be the slug for pages, date for archives, etc.

The problem with plain permalinks is that they don't use the request path, because this requires the standard .htaccess setup. Instead WordPress uses the query string to determine what to load.

So what you need to do is get the current query vars and append those to the URL. Thankfully those are also available in $wp, specifically as $wp->query_vars. This is an array of vars and values, so you can pass it directly to add_query_arg();:

global $wp;
echo add_query_arg( $wp->query_vars, home_url() );
Jacob Peattie
  • 44,319
  • 10
  • 50
  • 64
-1

Yeap, this should't work with plain format. and also just get_permalink() also not going to solved your issue here. To solved your issue you need to first correct Page/Post id and WordPress default doesn't offer us any function to do that. So you need to make one to grab the correct id. example

/**
 * Get correct page or post id
 *
 * @return int
 */
if ( !function_exists( 'get_the_real_ID' ) ) {
    function get_the_real_ID() {
      $post_id = 0;
      if (in_the_loop()) {
           $post_id = get_the_ID();
      } else {
        if ( is_front_page() && !is_home() ) {
            $frontpage_id = get_option( 'page_on_front' );
            $post_id = $frontpage_id;
        } elseif ( !is_front_page() && is_home() ) {
            $page_for_posts = get_option( 'page_for_posts' );
            $post_id = $page_for_posts;
        } else {
            global $wp_query;
            $page_object = $wp_query->get_queried_object();

            if ( is_page() || is_singular() ) {
                $post_id = $wp_query->get_queried_object_id();
            } else {
                $post_id = 0;
            }
        }
      }
      return $post_id;
    }
} 

Now just use this ID to grab your page/post url like this.

$id = get_the_real_ID();
$current_url = (empty($id)) ? home_url() : get_permalink($id);

I'm not sure that this is the best or perfect solution for your case or not but it should at-least will be work as you expected. I assume that you might change the permalink format or want to grab the proper url whatever format used? and uppon that I share this method. try and let me know.

mlimon
  • 400
  • 2
  • 11