I'm trying to create a category filter on my website that allows a user to select one category within two category lists. One list is called "Zones" and the other is called "Business types". When you click submit button on the form the page should refresh and only display the posts in the categories the users choose. This is the full code:
$zones = get_field('zones_list');
$business_types = get_field('business_type');
echo'<form role="search" method="post" id="searchform">
<div>';
foreach( $zones as $zone ):
echo'<input type="radio" value="'.$zone->slug.'" name="zone_list[]">'.$zone->name.'</input>';
endforeach;
echo'</div>
<div>';
foreach( $business_types as $business_type ):
echo'<input type="radio" value="'.$business_type->slug.'" name="business_type[]">'.$business_type->name.'</input>';
endforeach;
echo'</div>
<input type="submit" id="searchsubmit" value="Submit" />
</form>';
if(isset($_POST['zone_list'])) {
$list_name = $_POST['zone_list'];
$list_formated = implode('+', $list_name);
} else if (isset($_POST['business_type'])) {
$list_name = $_POST['business_type'];
$list_formated = implode('+', $list_name);
} else if (isset($_POST['zone_list'], $_POST['business_type'])) {
$list_name_1 = $_POST['zone_list'];
$list_name_2 = $_POST['business_type'];
$list_both = array_merge($list_name_1,$list_name_2);
$list_formated = implode('+', $list_both);
} else {}
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'category_name' => $list_formated,
'order' => 'DESC',
'posts_per_page'=> '3',
'paged' => get_query_var( 'paged' ),
);
global $wp_query;
$wp_query = new WP_Query( $args );
if( $wp_query->have_posts() ):
while( $wp_query->have_posts() ): $wp_query->the_post(); global $post;
Right now, the form filter only works if you select one category in one category list. If you select two categories(one in each list), which is what I need it to do, it returns NULL. I believe the error is located here:
else if (isset($_POST['zone_list'], $_POST['business_type'])) {
$list_name_1 = $_POST['zone_list'];
$list_name_2 = $_POST['business_type'];
$list_both = array_merge($list_name_1,$list_name_2);
$list_formated = implode('+', $list_both);
} else {}
I'm just learning PHP, so there might be a better way of doing this, but this is the only solution I've found so far. I really appreciate any help that I can get.
Aucun commentaire:
Enregistrer un commentaire