dede
dede

Reputation: 31

Using cpt and pass a variable using url

I want to create a nice readable permalink structure for my custom post type (CPT). My CPT "movie" has the following rewrite-slug movie/movie_name" (all works fine).

Now i want to add arg like this: movie/movie_name/arg and use arg in my template file as a php variable. But obvious it lead to not-found-page. How can i achieve this target?

edit: i want it in FRIENDLY URL format, it means i dont want to use GET for this.

Upvotes: 0

Views: 134

Answers (1)

Ihar Aliakseyenka
Ihar Aliakseyenka

Reputation: 14313

You may pass it like movie/movie_name?movie_arg=movie_value. It is will be available with $_GET['movie_arg']. Of course your need extra sanitization to handle this data.

To be able to read this in a WordPress way add params to a query_vars filter

function add_movie_arg_to_query_vars( $qvars ) {
    $qvars[] = 'movie_arg';
    return $qvars;
}
add_filter( 'query_vars', 'add_movie_arg_to_query_vars' );

Note: it should not be same as reserved WordPress query parameters

This way it will be available at your template with get_query_var('movie_arg')

print_r( get_query_var('movie_arg') ) // movie_value

More information here

Upvotes: 1

Related Questions