0

I am trying to add a simple filter for posts in my wordpress site by setting parameters in the URL, however it is simply not filtering the posts which is driving me crazy. All the tutorials or forums dont show what happens if the URL isnt filtering your posts.

My posts have categories (Test, windows and Wordpress) that I would like to learn to filter, e.g. it would have checkboxes for each category type. However, when the URL is set as /sites/?orderby=date&order=DESC&category%5B%5D=windows it isnt changing anything that is displayed?

If someone could help me out to understand why its not filtering the posts that would be appreciated.

My code:

    <form method="GET">

    <select name="orderby" id="orderby">
        <option
                value="date"
            <?php echo selected($_GET['orderby'], 'date'); ?>
        >
            Newest to Oldest
        </option>
        <option
                value="title"
            <?php echo selected($_GET['orderby'], 'title'); ?>
        >
            Alphabetical
        </option>
    </select>

    <input
            id="order"
            type="hidden"
            name="order"
            value="<?php echo (isset($_GET['order']) && $_GET['order'] == 'ASC') ? 'ASC' : 'DESC'; ?>"
    />


    <?php

    $terms = get_terms([
        'taxonomy' => 'category',
        'hide_empty' => false
    ]);

    foreach ($terms as $term) :

        ?>

        <label>

            <input
                    type="checkbox"
                    name="category[]"
                    value="<?php echo $term->slug; ?>"
                <?php checked(
                    (isset($_GET['category']) && in_array($term->slug, $_GET['category']))
                ) ?>
            />

            <?php echo $term->name; ?>

        </label>

    <?php endforeach; ?>


    <button type="submit">Apply</button>

</form>

<div class="wrapper">

    <?php
    $arg = array(
        'type' => 'post',
        'order' => 'ASC'
    );
    $blog_posts = new WP_Query( $arg );

    if ($blog_posts->have_posts()) :

        while( $blog_posts->have_posts() ) : $blog_posts->the_post();

            echo the_title();
            echo "<br>";

        endwhile;

    endif;
    ?>

</div>
  • You should have in your query args something like 'category' => 'cat-id' – Botond Vajna Jun 7 at 13:02
  • or 'category_name' => $term->name – Botond Vajna Jun 7 at 13:17
0

You have to filter the posts in the query arguments.

Try:

$mycat = $_GET['category'];

$arg = array(
        'type' => 'post',
        'order' => 'ASC',
        'category_name' => $mycat
    );

Just remove the square brackets from the link, or use: $mycat[0]

$mycat = $_GET['category'];

$arg = array(
        'type' => 'post',
        'order' => 'ASC',
        'category_name' => $mycat[0]
    );

| improve this answer | |
  • Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Jun 7 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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