0

I would like to set up a widget to filter my post by categories. Let's say, I do have two different categories, "Countries" and "length of stay" with sub categories. Here is an example of what I have:

  • Countries

    • China
    • Laos
    • Vietnam
  • Length of stay

    • 2-4 Days
    • 5-7 Days
    • 8-10 Days

What I want, is to filter posts by multiple categories. So, If the user is checking the country "Laos" and a length of stay of "2-4Days", I would like to retrieve only posts where the category "Laos" AND the category "2-4Days" has been attached.

I tried to use the Query Multiple Taxonomies pluging. However, this plugging is retrieving all posts with the "Laos" category and all posts with the length of stay of "2-4Days".

I know, that I can filter post with this query, but, I need some help to create this widget with the submit button. Also, I would like to customise it to remove parent categories and display them as a title (remove the checkbox of "Countries" and "Length of stay" and add to them a specific class)?

Working query:

<?php
// cat 42=Laos      cat 57=2-4Days
$my_query_1 = new WP_Query(
    array(
        'category__and' => array(42,57)
    )
);
while ($my_query_1->have_posts()) {
    $my_query_1->the_post();
    ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
        <?php the_title(); ?><?php the_excerpt(); ?>
    </a>
    <?php
}

Thanks for helping

2
  • Can you switch your code to while(){} instead of <?php while (): ?> <?php something(); ?><?php endwhile; ?> ? The mix of altsyntax and an opening closing php tags here there and everywhere makes it very difficult to see what's going on. Also WP_Query not WP_query – Tom J Nowell May 21 '13 at 9:35
  • I've removed the PHP tag spam and indented the code so it's readable. You should be indenting everything, makes it so much easier to do things – Tom J Nowell May 21 '13 at 9:40
-2

I thing, you should use this code for filter the check box category

<?php // cat 42=Laos      cat 57=2-4Days
    <?php $my_query_1 = new WP_query(array('category__and' => array(42,57))); ?>
    <?php while ($my_query_1->have_posts()) : $my_query_1->the_post(); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to
    <?php the_title(); ?>"><?php the_title(); ?><?php the_excerpt(); ?></a>
    <?php endwhile; ?>
1
  • 1
    Please properly format your code. File an edit and make proper use of the WYSIWYG editor to format your answer :-) – Pieter Goosen Dec 17 '14 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.