• Resolved eadwig

    (@eadwig)


    Hello

    I have to implement filters for the list of posts in every template page, so I embedded the code below as described in this article only to get a blank page upon clicking the submit button:

    <form class="post-filters">
    	<select name="orderby">
    		<?php
    			$orderby_options = array(
    				'post_date' => 'Order By Date',
    				'post_title' => 'Order By Title',
    				'rand' => 'Random Order'
    			);
    			foreach($orderby_options as $value => $label):
    				echo '<option '.selected($_GET['orderby'], $value).' value="'.$value.'">'.$label.'</option>';
    			endforeach;
    		?>
    	</select>
    	<input type="submit" value="Filter">
    </form>

    The article says ‘WordPress already knows what the order and orderby parameters mean and it uses them in the default query’, but apparently WP doesn’t and shows the following in the web address bar with no markup in the page (where ‘test’ is the name of the root directory):

    https://localhost:8888/test/?orderby=post_title

    Please someone tell me if WordPress can parse $_GET variables and if possible direct me to a tutorial which explains how post filters can be implemented without resorting to the use of a plugin?

    Thank you in advance.

Viewing 6 replies - 1 through 6 (of 6 total)
  • use this code snippet here you can get post title

    $selectedValue= 'Hello world!'; //you can set $_GET value here as $selectedValue = $_GET['orderby'];
    if(isset($_POST['submit']))
    {
    echo $_POST['postTitle'];//here you can get post id after form submission
    }
    $orderby_options = array(
    'post_date' => 'Order By Date',
    'post_title' => 'Order By Title',
    'rand' => 'Random Order'
    );
    $post =get_posts( $orderby_options );//get all post here
    ?>
    <form method="post">
        <select name="postTitle">
            <?php
            for($i = 0; $i < count($post); $i++) {
                $data = $post[$i];
                $postTitle = $data->post_title;
                $postId = $data->ID;
                ?>
                <option value="<?php echo $postId; ?>" <?php if($postTitle == $postTitle) echo 'selected="selected"'; ?>><?php echo $postTitle; ?></option>
                <?php
            }
            ?>
        </select>
        <input type="submit" name="submit" value="submit">
    </form>
    Moderator bcworkz

    (@bcworkz)

    WP recognizes only supported orderby arguments. “post_title” is not one of them. You want “title”. The supported arguments do not directly correspond to table column names or class properties as you apparently expected. The list of supported arguments is in the Codex.

    The default order is DESC, so your URL might be https://localhost:8888/test/?orderby=title&order=ASC to get normal alphabetical order A-Z and not Z-A.

    Thread Starter eadwig

    (@eadwig)

    Replacing ‘post_title’ with ‘title’ or pointing to https://localhost:8888/test/?orderby=title&order=ASC returned the same blank page with no code showing up in the page source. Also Search & Filter plugin returns the blank page. This made me think there may be something fundamentally incorrect with the code in the page template as shown at the top of this post. Does WP require a separate page template for post filtering?

    I also tried the code given by @magentomaster above and it generated a filter containing a list of post titles. However, clicking any of the post titles doesn’t sort the list.

    Some more suggestions would be appreciated.

    Thread Starter eadwig

    (@eadwig)

    The code inspector of Chrome returns the following message just below <option name=”orderby”>:

    Notice: Undefined index: orderby in /Applications/MAMP/htdocs/test/wp-content/themes/wwwares/front-page.php on line 17

    I note that orderby is the preset parameter, then what might be the message hinting?

    Moderator bcworkz

    (@bcworkz)

    I imagine line 17 of front-page.php contains a reference to $_GET['orderby'], which would be undefined for normal front page requests that do not include the orderby=title or similar URL parameter, such as for the initial request before an option is selected. You need more robust code to protect against this while still allowing the form to default to the selected value. I suggest adding code that specifies a default option when no orderby parameter is passed.

    $elected = array_key_exists('orderby', $_GET ) ? sanitize_text_field( $_GET['orderby']) : 'date';
    echo '<option '.selected( $selected, $value ).' value="'.$value.'">'.$label.'</option>';

    Note that your option keys should be ‘date’ and ‘title’ instead of ‘post_date’ and ‘post_title’.

    WP may or may not require a separate page for filtering. It depends on your theme and what page you want to filter. If the page you want to filter runs a normal WP query and loop that results in a list of posts, then supplying the correct URL parameters will filter the listed posts. If the page requested instead runs a custom query, it all depends on how the query is constructed. It could work, but most custom queries are there for a specific purpose which does not include filtering.

    Thread Starter eadwig

    (@eadwig)

    The post filter began working once the orderby parameter was set as variable in the argument of WP_Query() as in:

    $catFilter = array(
    'category_name' => $catName,
    'orderby' => $orderby
    );
    $catquery = new WP_Query($catFilter);

    I’m also including the code for the filter in case someone else runs into trouble (As @bcworkz suggested above, it is desirable to incorporate robustness in this.):

    <form class="post-filters">
    	<select name="orderby">
    		<?php
    			$orderby_options = array(
    				'post_date' => 'Order By Date',
    				'title' => 'Order By Title',
    				'rand' => 'Random Order'
    			);
    			foreach($orderby_options as $value => $label):
    				echo '<option '.selected($_GET['orderby'], $value).' value="'.$value.'">'.$label.'</option>';
    			endforeach;
    		?>
    	</select>
    	<input type="submit" value="Filter">
    </form>

    Concerning the blank page, setting ‘Your Homepage Displays’ to the latest posts and replacing front-page.php with index.php has fixed the blank page issue.

    Still I don’t understand why WP has to be set up this way, so I posted ‘Form started working once switched back to default reading settings but why?‘. It would be appreciated if someone could give me an answer in the new post.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘WordPress does not recognize $_GET post filters’ is closed to new replies.