Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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' => '&lt;&lt;',
        'next_text' => '&gt;&gt;'
    ) );
?>
</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;
}

?>
share|improve this question
    
What does SHIURIM_PAGE contains? What does get_page_link(SHIURIM_PAGE) returns? –  Max Yudin Feb 14 '13 at 12:52
    
How is this page created? show us you code? –  Bainternet Feb 14 '13 at 13:04
    
SHIURIM_PAGE is the page id. I will add the code to the questions. –  Amos N. Feb 14 '13 at 13:18

2 Answers 2

If you leave the action empty, the form will submit to the same page.

So just do this: <form action='' method='get'>";

share|improve this answer
    
Not working. Still falling to the index.php. The get_page_link(SHIURIM_PAGE) I put is a direct link to the page, but it not help. –  Amos N. Feb 14 '13 at 12:37
    
If I put method="post" it works, but then I have problem with the pagination parameters. –  Amos N. Feb 14 '13 at 12:38
    
You are probably using parameter names that are also used internally by wordpress. Make sure you make them unique! –  jberculo Feb 14 '13 at 13:53
    
My parameters are: search_rav, search_sidra and search_text. Are they used by Wordpress? –  Amos N. Feb 14 '13 at 14:18
    
I don't know. Sure there aren't any others? Try to check using Chrome or firebug what are the post variables... –  jberculo Feb 14 '13 at 16:11
up vote 0 down vote accepted

I found the problem and the solution.

The problem:

Using:

<form action="<?php echo get_page_link(SHIURIM_PAGE) ?>"  method="get"> 

is no good. In the generated HTML I can see:

<form action="http://mySite/?page_id=66" method="get">

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.

<form action="<?php echo home_url ?>"  method="get">
    <input type='hidden' name='page_id' value='66'>

Good luck.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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