0

I have googled a lot, but so far haven't come close to a proper solution yet. I need help with passing parameters to the homepage of a WordPress website. Here's my setup / requirements:

  1. I use a page template (say, MCTemplate) that is set as the static front page in Settings -> Reading.

  2. I use 'Post name' permalink structure.

  3. I need to pass a single parameter (say, mcdata) to the static homepage (i.e., MCTemplate).

I have used the following code in functions.php, but when I pass the parameter to the homepage (e.g., http://www.example.com/?mcdata=helloword), it show a list of latest posts instead of static front page.

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

Using front-page.php comes close to solving this, but in that case I cannot use a static front page. Are there any rewrite rules or anything that I should change to get it working with the above requirements? All help is appreciated.

  • I removed the comment and create an answer. Thank you. – gmazzap Sep 10 '13 at 14:20
3

front-page.php is a static front page (if you rename your current template front-page.php, and don't select nothing in Settings->Reading it works exactly like now.

However, if you want to use the page approach and you don't use mcdata for query, remove the filter you posted, and simply retrieve the mcdata value in your template using $_GET['mcdata'];.

  • The confusion was due to two queries: the WordPress loop query (WP_Query) and the URL query string. Thanks for the quick clarification. – Vasu Adiga Sep 10 '13 at 14:42
  • Knowing that the site front page was a 'static' page, and then finding this, was the solution for me too. Trying to set a 'query_vars' hook for the query parameters I wanted to look at disrupts existing code using is_front_page(), and prevented the static front page from displaying. – jrg May 29 '15 at 15:18
0

To pass parameter on the front page static I use the prefix "ch_" as follow:

http://www.example.com/?ch_mcdata=helloword

in this way it will work.

  • 1
    What is the significance of the ch_ prefix? – shshaw Sep 25 '14 at 18:01
  • nothing to do specifically with ch_ its because the query_var isn't registered with ch_ in front so WordPress doesn't try and process it. You could use any characters before it to get it to work. – Alex Older Sep 22 '16 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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