phpwordpressurlcustom-post-typeurlvariables

Prevent WordPress to remove my variables added in my url (custom post type)



Do you know why WordPress delete variables when I'm on a page of "custom post types" (singular) and not when I'm on a standard page?

Here is how I create my URL in a loop.

<a href="<?php echo esc_url( add_query_arg( 'p', $url, the_permalink() ) ); ?>">See more</a>

The link is generated correctly, but when I click "?p=value" is deleted...

Thanks in advance! :)


Solution

  • Issue is that you're using "the_permalink()" which outputs the value itself.

    You need to use get_the_permalink() function.

    Your new code will look like this:

    <a href="<?php echo esc_url( add_query_arg( 'a', $url, get_the_permalink() ) ); ?>">See more</a>
    

    Update:

    Plus, you're trying to retain ?p=xxx in your url. WordPress takes p as a post id variable, and thus it uses it and uses permalink rewrite rules to redirect and remove this from the url.

    You will retain the query variable if you use anything other than ?p= , try ?a=something e.g.