0

I am trying to send value from one page to another page in WordPress as i do not want to repeat logic to fetch that value in other page. I am not very proficient in WordPress/PHP, if i have to do that in Java, i will create a hidden field and pass that value to server side code and from there can again set it as a request parameter, not sure how to achieve that in WordPress with help of PHP.

I have a page and a link "see more" on right hand navigation panel,once user click link, he/she is being redirected to another page ,but I need to show information based on values from the parent page

User is being redirected based on some rewrite rule and i am picking few values from using get_query_var().

I can do that by adding that parameter as query parameter, but i do not want to change current URL being generated.

Here is more information about my use case:

Site on which i am working is related to travelling and i am showing destination recommendations on few pages on its right hand side bar, for the UI layout, i am not showing all recommendations but few say "10" and than i am giving a link saying "Check all recommendations".

When user land on certain page, i am fetching categories assigned to that post and it is possible to have multiple categories say

  1. hill-station
  2. adventure etc

So based on these categories, i am showing them recommendations, so when user click on the click i want to pass this category information to next page so as i can show them eligible destination list.

Is there any other way to pass value to new page except for setting value in session?

2
  • I don't think this question is very clear. There is a lot of context missing. You don't explain why you want to do this, and why is at least as important as what maybe more important. However, if you don't want to change the URL then sessions is a way to do it, or cookies.I don't think this question is very clear but if you don't want to change the URL sessions is a way to do it, or cookies.
    – s_ha_dum
    Commented Sep 8, 2013 at 18:00
  • @s_ha_dum: thanks for the input, i have updated my question and tried to provide more details to make it more clear Commented Sep 8, 2013 at 18:07

1 Answer 1

1

Simply use a $_POST variable to set the terms to the page that handle the recomendations and use an hook into pre_get_posts to set the query.

I write a function that output the link, and you can use it as a template tag.

Add this function in a plugin or in your functions.php

function more_recomentation_link() {
  if ( ! is_single() && ! is_page() ) return;
  $post = get_queried_object();
  $cats = wp_get_object_terms( $post->ID, 'category', array('fields' => 'ids') );
  if ( ! empty($cats) ) {
    // I don't know which is the url that handle the recomandation showing
    // so I use home_url() change it with rigth url
    echo '<form method="post" action='. home_url() . ' id="see-more-form" style="display:none">';
    foreach ( $cats as $cat ) {
      echo '<input type="hidden" name="cats[]" value="' . $cat. '" />';
    }
    echo '</form>';
  }
  echo "<a href='javascript:void(0)' onclick='document.getElementById(\"see-more-form\").submit();return false;'>" . __('See More') . "</a>";
}

function set_recomendations_query( $query ) {
  if ( ! is_admin() && is_main_query() && isset($_POST['cats']) && ! empty($_POST['cats']) ) {
    $tax_query = array(
      'taxonomy' => 'category',
      'field' => 'id',
      'terms' => $_POST['cats']
    );
    $query->set('tax_query', array($tax_query));
    $query->set('posts_per_page', -1);
  }
}
add_action('pre_get_posts', 'set_recomendations_query');

The in the template, even in sidebar, show the 'See More' link simply using

<p><?php more_recomentation_link(); ?></p>
2
  • i tried your code, but unable to get value in next page...not sure about my_custom_vars function usage Commented Sep 8, 2013 at 18:52
  • @UmeshAwasthi see the edit. You have only to change tha url that handle the recomendation view in the more_recomentation_link function
    – gmazzap
    Commented Sep 8, 2013 at 19:00

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.