Post Filtering by GET URL parameters

I am trying to add a simple filter for posts in my wordpress site by setting parameters in the URL, however it is simply

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>

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>
Share Improve this question asked Jun 7, 2020 at 12:21 ThomasThomas 11 bronze badge 2
  • You should have in your query args something like 'category' => 'cat-id' – Botond Vajna Commented Jun 7, 2020 at 13:02
  • or 'category_name' => $term->name – Botond Vajna Commented Jun 7, 2020 at 13:17
Add a comment  | 

1 Answer 1

Reset to default 1

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]
    );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742368378a4430771.html

相关推荐

  • Post Filtering by GET URL parameters

    I am trying to add a simple filter for posts in my wordpress site by setting parameters in the URL, however it is simply

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信