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
1 Answer
Reset to default 1You 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
评论列表(0条)