I’m making my own theme. In my theme I have a page that display custom post using a custom query.
I added a form to this page. The form contain 2 comboboxes that contain values of meta data of my custom post. I want that when the user select values from this comboboxes and click the submit, WordPress will show again the same page and will add the submit parameter to my custom query (something like filtering a table).
The problem is that when clicking on the submit, WordPress falling down to the index.php instead to my page.
My form declaration is:
<form action='"<?php echo get_page_link(SHIURIM_PAGE)?> "' method='get'>";
Should I put something else in the action?
Thank is advanced.
My full page code:
<?php
/*
Template Name: Shiurim page
*/
require 'search-form.php';
?>
<?php get_header();
global $THEME_PATH;
global $IMAGES_PATH;
?>
<link rel="stylesheet" type="text/css" href="<?php _e($THEME_PATH)?>/shiurim.css">
<div id="page-header-pic">
<img src='<?php _e($IMAGES_PATH)?>/contact-header.jpg' alt="header picture" />
</div>
<div class="page-title">
<?php wp_title(); ?>
</div>
<div class="areaBg">
<div class="page-content-full">
<div class="panel">
<div class="panel-title"></div>
<div class="panel-content">
<div id="pageContentId">
<?php
function getParam($paramName, $defaultValue) {
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
if (!empty($_GET[$paramName])) {
return $_GET[$paramName];
}
}
else { // it's a post
if (!empty($_POST[$paramName])) {
return $_POST[$paramName];
}
}
return $defaultValue;
}
$pageIndex = getParam('paged', 0);
$searchRav = getParam(SEARCH_RAV, 0);
$searchSidra = getParam(SEARCH_SIDRA, 0);
$searchText = getParam(SEARCH_TEXT, "");
$searchText = trim($searchText);
$queryArgs = array (
'post_type' => array( POST_TYPE_SHIUR ),
'orderby' => 'title',
'order' => 'ASC',
'paged' => $pageIndex
);
$paginateSearchArgs = array();
$metaQuery = array();
if ($searchRav != 0) {
$metaQuery[] = array(
'key' => POST_TYPE_RAV,
'value' => $searchRav
);
$paginateSearchArgs[SEARCH_RAV] = $searchRav;
}
if ($searchSidra != 0) {
$metaQuery[] = array(
'key' => POST_TYPE_SIDRA,
'value' => $searchSidra
);
$paginateSearchArgs[SEARCH_SIDRA] = $searchSidra;
}
if (count($metaQuery) > 0) {
$queryArgs['meta_query'] = $metaQuery;
}
if (! empty($searchText)) {
$queryArgs['s'] = $searchText;
$paginateSearchArgs[SEARCH_TEXT] = $searchText;
}
?>
<div id="shiurimSearchPanel">
<?php renderSearchFrom($searchRav, $searchSidra, $searchText); ?>
</div>
<div>
<?php
//query_posts($queryArgs);
$shiurim_query = new WP_Query( $queryArgs );
global $wp_query;
// Put default query object in a temp variable
$tmp_query = $wp_query;
// Now wipe it out completely
$wp_query = null;
// Re-populate the global with our custom query
$wp_query = $shiurim_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => '<<',
'next_text' => '>>'
) );
?>
</div>
<?php
$is_first = true;
if ( $shiurim_query->have_posts() ) : while ( $shiurim_query->have_posts() ) : $shiurim_query->the_post();
?>
<?php if (!$is_first) { ?>
<div class='shiurSeperator'></div>
<?php } ?>
<?php
$is_first = false;
get_template_part( "content", "shiur-list" );
?>
<?php endwhile; else: ?>
<p><?php _e('Nothing is found'); ?></p>
<?php
endif;
// Restore original query object
$wp_query = $tmp_query;
// Be kind; rewind
wp_reset_postdata();
?>
</div>
</div>
</div>
</div>
<div class="clear"></div>
</div>
<?php get_footer(); ?>
search-form.php (I’m using it in the index.php as well).
<?php
/**
* Search panel
*/
function renderSearchFrom($searchRav = array(), $searchSidra = array(), $searchText = "") {
$rabanimList = getPostArrayResult(POST_TYPE_RAV);
$sdatorList = getPostArrayResult(POST_TYPE_SIDRA);
$html = "<div id='search-panel'>";
$html .= " <form action='" . get_page_link(SHIURIM_PAGE). "' method='post'>";
$html .= " By rav";
$html .= " <select name='search_rav'>";
$html .= " <option value='0'>All ravs</option>";
foreach ($rabanimList as $rav) {
$isSelected = ' ';
if ($rav['id'] == $searchRav) {
$isSelected = ' selected ';
}
$html .= "<option value='" . $rav['id'] . "'" . $isSelected . ">" . $rav['title'] . "</option>";
}
$html .= "</select>";
$html .= " By sidra";
$html .= " <select name='search_sidra'>";
$html .= " <option value='0'>All sidras</option>";
foreach ($sdatorList as $sidra) {
$isSelected = ' ';
if ($sidra['id'] == $searchSidra) {
$isSelected = ' selected ';
}
$html .= "<option value='" . $sidra['id'] . "'" . $isSelected . ">" . $sidra['title'] . "</option>";
}
$html .= "</select>";
$html .= " Free text";
$html .= " <input type='text' name='search_text' class='searchFreeText' value='" . $searchText . "'/>";
$html .= " <input type='submit' value='Search' />";
$html .= " </form>";
$html .= "</div>";
echo $html;
}
?>
If you leave the action empty, the form will submit to the same page.
So just do this:
<form action='' method='get'>";
You can use this way, it´s shorter and works fine:
I found the problem and the solution.
The problem:
Using:
is no good. In the generated HTML I can see:
But when submitting the form, the ?page_id=66 is disappear. So WordPress don’t know to redirect the request to my page template.
The solution:
The action will point to the home page and add hidden field that will hold the page_id.
Good luck.
Another way to post a form to the same page is using php server variables:
Using the_permalink() function also could fail, because maybe you are inside a loop and the_permalink() returns the permalink of the $post object currently processed by the loop itself.