-
-
Save Quin452/6e4fcae4b2506f629362c9423e8d33f4 to your computer and use it in GitHub Desktop.
// What were searching for | |
$args = array( | |
'post_type' => $post_type, | |
'post_status' => 'publish' | |
); | |
// Now, if the category has been selected AND it doesn't equal the "show all" value | |
if(isset($_POST['category']) && $_POST['category'] != -1) { | |
// Explode the string into an array | |
$categories = array_map('intval', explode(',', $_POST['category'])); | |
// Then load the POST array into the cat args | |
$args['category__in'] = $categories; | |
} | |
// Yep, a query | |
$query = new WP_Query($args); | |
// Loop through | |
if($query->have_posts()) { | |
// Do your thing here | |
} |
Hi Implement your code, but all post are still not displaying when I choose "All" option. Please see below.
$category = $_POST['category'];
if( in_category( 'category_1' ) ):
$args = array(
'post_status' => 'publish',
'post_type' => 'post',
'posts_per_page' => -1,
'child_of' => 102,
'category_name' => 'cat1,cat2,cat3,cat4,cat5'
);
elseif(in_category( 'category_2' )) :
$args = array(
'post_status' => 'publish',
'post_type' => 'post',
'posts_per_page' => -1,
'child_of' => 101,
'category_name' => 'cat_1,cat_2,cat_3'
);
endif;
/* if (isset($category)){
$args['category__in'] = array($category);
}*/
if(isset($category) && $category != -1) {
// Explode the string into an array
$categories = array_map('intval', explode(',', $category));
// Then load the POST array into the cat args
$args['category__in'] = $categories;
}
$query = new WP_Query($args);
And here's the html/php:
<form class="js-filter-form" method="post">
<select name="categories">
<?php
if( in_category( 'category_2' ) ):
$cat_args = array(
'post_type' => 'post',
'option_all' => 'All',
'exclude' => array(1),
'child_of' => 101
);
elseif(in_category( category_1' )) :
$cat_args = array(
'post_type' => 'post',
'option_all' => 'All',
'exclude' => array(1),
'child_of' => 102
);
endif;
$categories = get_categories($cat_args); ?>
<option class="js-filter-item" value="-1">All</option>
<?php foreach ($categories as $cat) : ?>
<option class="js-filter-item" value="<?= $cat->term_id; ?>"><?= $cat->name; ?></option>
<?php endforeach; ?>
</select>
</form>
@User0123456789 okay, so first you've got $category = $_POST['category'];
The POST data is categories, according to your HTML (either change the HTML to categories, or the POST to category).
You also need to have some if statement here, to check that something has been submitted, otherwise it'll return an error.
Once you can submit the category properly (I recommend outputting/pre the POST data), then you can use that in the WP_Query args.
I honestly cannot tell what you are doing with $cat_args and the category 1/2.
@Quin452 I tried to change the <select name="categories">
to <select name="category">
but all the posts in other options are not displayed. Do you mean if statement in html/php file?
I used cat_args
here
$categories = get_categories($cat_args); ?>
<option class="js-filter-item" value="-1">All</option>
<?php foreach ($categories as $cat) : ?>
<option class="js-filter-item" value="<?= $cat->term_id; ?>"><?= $cat->name; ?></option>
<?php endforeach; ?>
and for the category 1 and 2, and I use it to show different categories depends in the page slug name.
@User0123456789 do you have a link to your site I can look at?
@Quin452 it's still in my local env. But I can post the js code here. Wait a moment
Regarding this comment