php - 在 WordPress 中使用复选框过滤输出

标签 php jquery wordpress filter

我正在 WordPress 中构建一个带有过滤功能的页面。基本上我想在单击复选框时更改数据的输出。我现在拥有的:

    <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(); ?>

但是上述解决方案不起作用,我无法找出问题所在。页面重新加载,URL 显示所选值 /?order=order

最佳答案

我认为问题在于表单方法,您在代码中使用 $_POST 但通过输入特定表单 method = "post" 您可以得到这个使用 $_POST 方法工作。

默认情况下,如果没有 method 属性,它将在提交表单时将您带到 get 方法。

关于php - 在 WordPress 中使用复选框过滤输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20740546/

相关文章:

php - MYSQL 查询 : 3 Table UNION, 最后 100 个结果按 ASC 顺序

php - 无法在 Docker 中使用 PHP 连接到 Elasticsearch

php - php 中的隐藏密码

wordpress - 从我的自定义帖子类型列中删除自定义分类列

sql - MySQL 代码无法显示类别名称(WordPress 数据库)

php - 替换单词并保持找到的字符串区分大小写

javascript - 如果根/父元素向上/向下滚动则显示按钮,如果不是则隐藏该按钮

javascript - 在 jQuery 中获取父元素

查询 :after selector is this possible?

javascript - jQuery - 如何从 ajax 请求获取元素的类