Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Passing GET array variables to page template WordPress

  • REFUNDED

I created a custom search form to filter taxonomies and custom fields meta by make use of wp_query.

The form action is redirect to a custom page template where the custom wp_query running. The search form is using get method to passing the variable.

The form is something like this

<form method="get" id="customsearch" action="<?php echo get_page_link( 12 ); ?>/">//action point to the custom page template

//these are to collect taxonomy data
<input type="hidden" name="taxo[1][name]" value="os">
<input type="text" name="taxo[1][term]" value="">
<input type="hidden" name="taxo[2][name]" value="ram">
<input type="text" name="taxo[2][term]" value="">
<input type="hidden" name="taxo[3][name]" value="cpu">
<input type="text" name="taxo[3][term]" value="">

/these are to collect custom field meta data
<input type="hidden" name="cmf[1][name]" value="place">
<input type="text" name="cmf[1][value]" value="">
<input type="hidden" name="cmf[2][name]" value="price">
<input type="text" name="cmf[2][value]" value="">
<input type="hidden" name="cmf[3][name]" value="software">
<input type="text" name="cmf[3][value]" value="">

<input type="submit" class="submit" value="Search" alt="[Submit]" name="submit" title="Search" />
</form>


And i added the variable to query_vars:

add_filter('query_vars', 'my_queryvars' );

function my_queryvars( $qvars )
{
$qvars[] = 'taxo';
$qvars[] = 'cmf';
return $qvars;
}


The problem is, when the permalink structure set to default, the form was not redirected to the desired page, instead it went to homepage.

If I set the permalink other structure, like Numeric or Post name, it goes to the correct page, but url broken in paginated page. Eg:
First page:
<blockquote>page_url/?taxo[1][name]=os&taxo[1][term]=xxx&taxo[2][name]=ram&taxo[2][term]=cpu&cmf[1][name]=price&cmf[1]&cpf[1][value]=xxx&cmf[2][name]=place.......submit=Search</blockquote>
to
<blockquote>page_url/page/2/?submit=Search (the GET data stripped)</blockquote>

How do I solve this problem?

Answers (3)

2012-10-26

Clifford P answers:

Maybe this will help: [[LINK href="http://wpquestions.com/question/showLoggedIn/id/7472"]]http://wpquestions.com/question/showLoggedIn/id/7472[[/LINK]]


house.tc comments:

I tried to add the rewrite rules, but still not working.

This is what I had tried:
function add_author_more_rewrite_rule() {

add_rewrite_rule('page_url/(\d*)$','index.php?taxo=$matches[1]&cmf=$matches[2]', 'top' );
}

add_action('init','add_author_more_rewrite_rule');


Is this because the variables are in arrays?


Clifford P comments:

Someone else will have to chime in. I was just trying to point you in the right direction. Good luck.

2012-10-26

Francisco Javier Carazo Gil answers:

To be able to leave RewriteRule in .htaccess you will need to use them directly in the PHP code, look at the end of this entry:

http://codex.wordpress.org/Custom_Queries

In your case could be something like this:

add_action('generate_rewrite_rules', 'geotags_add_rewrite_rules');

function geotags_add_rewrite_rules( $wp_rewrite )
{
$new_rules = array(
'page_url/(.+)' => 'index.php?page_url=' .
$wp_rewrite->preg_index(1) );

/​/​ Add the new rewrite rule into the top of the global rules array
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}


house.tc comments:

Tried, but still got no luck.


Francisco Javier Carazo Gil comments:

You have to redirect the template you load as a function of the query_vars you receive in the index.php of your theme.


house.tc comments:

Sorry I don't quite get what you mean. Are you mean that I need to redirect to my page template page from my homepage?


Francisco Javier Carazo Gil comments:

I would do the next:
1. Do the rewrite as I told you and redirect all to index.php
2. Collect query_vars and if the var you want is set, load your template


house.tc comments:

Tried also, but still cannot make it right. Anyway, I figured another approach to solve this problem. Now I using pre_get_posts to change the search query. And it work.

2012-10-27

jazbek answers:

Hi there. Have you looked into using the paginate_links() function of WordPress to create your pagination?
http://codex.wordpress.org/Function_Reference/paginate_links

See if this code works:


echo paginate_links(array(
'total' => $wp_query->max_num_pages,
'current' => max( 1, get_query_var('paged') ),
'show_all' => true,
'type' => 'list',
'add_args' => $_GET,
'next_text' => '&raquo;',
'prev_text' => '&laquo;'
)) ?>


house.tc comments:

For some reason, I want to use the default next_posts_link() for the pagination. Anyway, thanks for your suggestion.