In the above URL - how do I tell Wordpress that 'product' is a variable and 'somename' is its value.

Then how do I extract that value and use it inside a function I'll create?

In short I need to: register variable, check url for variable, extract value, use default post template and generate the_content based on that value, but only if there is a value and only for that url.

Right now I'm neither able to register/extract the variable (when I visit the above-mentioned URL I get a 404) nor specify that the_content should only be replaced when the url contains that variable - it simply replaces the content on all regular posts.

Here's what I came up with so far:

function product_rewrite_rules(){
add_action( 'init', 'product_rewrite_rules' );

add_filter( 'query_vars', 'setup_filter_query_vars_product' );
function setup_filter_query_vars_product( $query_vars ){
    $query_vars[] = 'product';
    return $query_vars;

function products_page () {
global $wp_query;
if( isset( $wp_query->query_vars['product'] )) {
$productvalue = $wp_query->query_vars['product'];

echo "product is $productvalue";
add_filter ('the_content', 'products_page');  

What am I doing wrong?

  • How are you setting these up, as child pages? Commented Feb 21, 2014 at 16:58

1 Answer 1


My recommendation is to register a custom post type. You can call it products, and then all posts placed under Products will get the http://domain.com/products/some-product rewrite. Once it's its own post type, you can specify a custom template for it, single-product.php, and you can add any custom fields, classes, etc. from there.

  • I wanted to use the post template so I don't have to edit two templates whenever I make some changes to the design. Is there a way to do so, or I should go ahead and create a new template?
    – kat
    Commented Feb 21, 2014 at 17:08
  • You can definitely keep the same template if you don't have any extra data to add in. Even if you do, you can do if ( 'product' == get_post_type() ) {} in your single.php. The body class body.product will allow you to add product specific styles as well. Commented Feb 21, 2014 at 17:11
  • OK. In that case why doesn't anything work - neither the URL gets registered, nor the if( isset( $wp_query->query_vars['product'] )) condition is being honored? I'm completely in the dark. Everything seems like what others are doing.
    – kat
    Commented Feb 21, 2014 at 17:16
  • Is the product loading? Also, use get_post_type() or $wp_query->query_vars['post_type'], it would never be $wp_query->query_vars['product']. Commented Feb 21, 2014 at 17:46
  • Why $wp_query->query_vars['post_type'] when I don't have a post_type variable anywhere? What I want http://domain.com/products/some-product to be converted to/interpreted as is this http://domain.com/index.php?product=some-product then I can tell Wordpress to extract the value and generate the_content whenever someone calls the ?product variable.
    – kat
    Commented Feb 21, 2014 at 18:22

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.