Hi Stack Overflow community.
I'm developing a wordpress website for Satan. First of all, I'm a junior webdeveloper, so let's say that I still have a lot to learn... :-) This is the first time I'm working with Ajax, and I don't have much experience with PHP or SQL outside wordpress.
This website has a search function based on checkboxes. I have 2 groups of 3 checkboxes (so, 6 in total). So, I have 2 taxonomies and each checkbox represents a different term. I want to change the contents of the page using ajax, based on the categories Satan chooses using these checkboxes. I was able to do this, but only for one combination of checkboxes at a time. Because there are dozens of different combinations, I don't think that writing dozens of different queries/args (one for each combination) is a good policy. So, how can I do this?
Let's see an example, for you to understand what I mean:
Imagine that Satan wants to check the number of souls in Hell trough a form. There's two groups of checkboxes, one for people and one for punishments, something like this:
People
- Men
- Women
- Children
Punishment
- In flames
- Impaled
- Being eaten by demons
Now, I have this jQuery Ajax code:
$('#searchsouls').click(function(){
$.ajax({
url:"<?php echo get_bloginfo('template_url'); ?>/evil-laugh.php",
type:"POST",
async:"false",
data:$("#de-form").serialize(),
success:function(data) {
$('#666').empty().html(data);
},
error:function(e){
console.log('Error: ' + e);
}
});
return false;
})
This is working just fine. Then, I have this code inside evil-laugh.php:
<?php global $query_string;
$args = wp_parse_args($query_string);
$args = array(
'post_type' => 'souls',
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
'post_status' => 'publish',
);
?>
<!-- THE LOOP -->
<?php $wp_query = new WP_Query( $args ); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
This, of course, shows all the souls and all kinds of punishments, nothing fancy. I know that I could accomplish what Satan wants by defining a different set of arguments for each combination of categories. Something like (suppose that the "Impaled" term has an ID of 9, and that "cat9" is the name of the checkbox):
if ($_POST['cat9']) {
$args = array(
'post_type' => 'souls',
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
'post_status' => 'publish',
'punishment' => 'impaled',
);
However, there are dozens of different combinations. Here's a few examples:
- Men impaled
- Women OR children being eaten by demons
- Men OR women in flames OR impaled
- Children in flames OR impaled OR being eaten by demons
I suppose I could do some sort of "for loop" to accomplish this, but I have no idea how to do this or how I could add the category IDs to the wordpress query, defining the respective ANDs and ORs.
I could really appreciate some help in this issue. However, have in mind that I don't want just a solution, I want to understand what I'm doing and learn a few new things if possible.
Thanks in advance.