2

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.

4

1 に答える 1

6

さて、私は自分で解決策を見つけることができました。それがこの種の問題に対する最も堅牢な解決策であるかどうかはわかりませんが、誰かが同じことを探している場合に備えてここに行きます。実用上の理由から、各チェックボックスの名前を関連するカテゴリの名前に変更しました。たとえば、name="Men"他のカテゴリと同じように、[男性]チェックボックスに使用しました。コードは次のとおりです。

if ($_POST['men']) {
    $args['people']='men';
}   

if ($_POST['women']) {
    if ($_POST['men']) {
        if ($_POST['children']) {
            $args['people']='men, women, children';
        } else {
            $args['people']='men, women';
        }   
    } else {
        $args['people']='women';
    }   
}

if ($_POST['children']) {
    if ($_POST['women']) {
        if ($_POST['men']) {
            $args['people']='men, women, children';
        } else {
            $args['people']='women, children';
        }   
    } elseif ($_POST['men']) {
        $args['people']='men, children';
    } else {
        $args['people']='children';
    }   
}

もちろん、罰のカテゴリには同じコードがありますが、用語は異なります。コードにはまだ何らかの冗長性があると思いますが、うまく機能します。後で確認する必要があります。確かに、これは3つのカテゴリでのみ正常に機能します。カテゴリがたくさんある場合は、このような楽しいプログラミングはあまりできないので、「forcycle」などの使用方法を検討することをお勧めします。乾杯!

更新:新しくてより良いソリューション

ついに私はなんとかこのプロジェクトに戻ることができました。PHPについてもう少し学ぶことができただけでなく、コードがはるかに堅牢になり、必要な数のカテゴリ用語を追加できるようになりました。ここに行きます:

$people_args = array(
    'type' => 'souls',
    'taxonomy' => 'people',
    'orderby' => 'name',
    'order' => 'ASC',
);
$people = get_categories($people_args);
$arr_people = array();
foreach ($_POST as $key => $value) {
   foreach($people as $person){
        if($person->slug == $key){
            array_push($arr_people, $key);
        }
   }            
}
$values_people = implode(",", $arr_people);
$args['people']=$values_people;

$punishment_args = array(
    'type' => 'souls',
    'taxonomy' => 'punishment',
    'orderby' => 'name',
    'order' => 'ASC',
);
$punishments = get_categories($punishment_args);
$arr_punishment = array();
foreach ($_POST as $key => $value) {
   foreach($punishments as $punishment){
        if($punishment->slug == $key){
            array_push($arr_punishment, $key);
        }
   }            
}
$values_punishment = implode(",", $arr_punishment);
$args['punishment']=$values_punishment;

誰かがこれに興味があるかどうかはわかりませんが、これまでの私の解決策は次のとおりです。これは、チェックボックスを使用してカテゴリ別にコンテンツを表示する完全に機能するAJAXの方法です。Webサイトが完成したらすぐに、ここにURLを配置して、必要に応じてこのコードをテストできるようにします。知識は力である。

于 2012-04-09T09:31:12.950 に答える