2

I'm building a page in WordPress with a filter function. Basically I want to change the output of the data when you click on the checkbox. What I have now:

    <form class="compo-filter">
    <input type="checkbox" name="compo-cat" value="indoor" onclick="if(this.checked){this.form.submit();}" />Indoor
    <input type="checkbox" name="compo-cat" value="outdoor" onclick="if(this.checked){this.form.submit();}" />Outdoor
    <input type="checkbox" name="compo-cat" value="men" onclick="if(this.checked){this.form.submit();}" />Men
    <input type="checkbox" name="compo-cat" value="women" onclick="if(this.checked){this.form.submit();}" />Women 
    <input type="checkbox" name="order" value="order" onclick="if(this.checked){this.form.submit();}" />Show only competitions currently in progress
</form>

<?php 
    $today = date('Y-m-d');
    $args = array(
        'post_type' => 'competitions',
        'posts_per_page' => 10,
        'meta_key' => 'startdate',
        'meta_compare' => '>=',
        'meta_value' => $today,
        'orderby' => 'startdate',
        'order' => 'ASC'
    );
    $args2 = array(
        'post_type' => 'competitions',
        'posts_per_page' => 10,
        'meta_key' => 'startdate',
        'orderby' => 'startdate',
        'order' => 'ASC'
        );

    if ($_POST['order'] == 'order') {   
        $query = new WP_Query($args2);
    } else {
        $query = new WP_Query($args);
    }   

    if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>

The above solution does however not work, and I'm not able to work out what's going wrong. The page reloads, and the URL is showing the selected value /?order=order.

3 Answers 3

2

I think the issue lies with the form method, you are using $_POST in the code but by putting the specific form method = "post" you can get this working in $_POST method.

By default without method attribute it will take you to the get method while submitting the form.

Sign up to request clarification or add additional context in comments.

Comments

1

Solved my own problem. Needed to use $_GET in stead of $_POST.

3 Comments

Thanks for posting you answer, that will hopefully help people in the future. For your information, if you wanted to use $_POST (which is usually more desirable as there is no ugly query string visible to users), you can change the line <form class="compo-filter"> to <form class="compo-filter" method="POST">.
Thanks, this solution is indeed better since I don't want the string to be publicly visible.
Good news. Another fairly common technique is to POST your form to a handler (a non-visible page) that does the required work and then redirects the user to the relevant page. This is particularly useful if you are updating a post for example. The main reason for this is security, so if you are not doing any database writes or similar, it may not be applicable, but hopefully it'll be good to know for the future...
0

I think the issue lies with the form method, you are using $_POST in the code but by putting the specific form method = "post" you can get this working in $_POST method.

By default without method attribute it will take you to the get method while submitting the form.

Solved my own problem. Needed to use $_GET in stead of $_POST.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.